I have a spreadsheet whereby it calls a SQL Server Stored procedure and returns data. Works 100% in Excel 2010 and has done for months. However, for some users on Excel 2013, it does not return all the data - it just brings through 0's. Here is my code:
Sub Button10_Click()
Set con = New ADODB.Connection
Set cmd = New ADODB.Command
Set rs = New ADODB.RecordSet
Sheets("Customer").Visible = True
'Unprotect worksheet
Worksheets("Customer").Unprotect Password:="*******"
' remove and re-add auto filter
Sheets("Customer").AutoFilterMode = False
Sheets("Customer").Range("A4:AO4").AutoFilter
Application.DisplayStatusBar = True
Application.StatusBar = "Contacting SQL Server..."
' Remove any values in the cells where we want to put our Stored Procedure's results.
With Sheets("Customer")
.Rows(5 & ":" & .Rows.Count).Delete
End With
' Log into our SQL Server, and run the Stored Procedure
con.Open "Provider=SQLOLEDB;Data Source=*********;Initial Catalog=**********;Integrated Security=SSPI;Trusted_Connection=Yes;"
cmd.ActiveConnection = con
Application.StatusBar = "Running stored procedure..."
'===== Check if the user has ticked the 'Include Current Month Totals checkbox ====
If Range("$M$1") = True Then
'include current month in totals
cmd.CommandText = "usp_Customer_Lvl1_Current_Month_INC"
Else
'do not include current month in totals
cmd.CommandText = "usp_Customer_Lvl1_Previous_Month_INC"
End If
Set rs = cmd.Execute(, , adCmdStoredProc)
'==========
Sheets("Customer").Range("A5").CopyFromRecordset rs
Application.Goto Reference:=Worksheets("Customer").Range("A5")
'protect worksheet
Worksheets("Customer").Protect Password:="***********", AllowFormattingCells:=True, AllowFiltering:=True
'=============
rs.Close
Set rs = Nothing
Set cmd = Nothing
con.Close
Set con = Nothing
Application.StatusBar = "Data successfully updated."
End Sub
It has to be something in the VBA. The data is there in SQL and Excel 2010 - any ideas?