【发布时间】:2017-06-13 14:39:51
【问题描述】:
我有一个电子表格,它调用 SQL Server 存储过程并返回数据。在 Excel 2010 中 100% 工作并且已经完成了几个月。但是,对于 Excel 2013 上的某些用户,它不会返回所有数据 - 它只是带入 0。这是我的代码:
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
它必须在 VBA 中。 SQL 和 Excel 2010 中有数据 - 有什么想法吗?
【问题讨论】:
-
有错误提示吗?
-
不 - 没有 - 它只是不返回所有数据 - 非常奇怪
-
查看我的past answer。在那里您可以找到关于 SP 的非常有用的信息。
-
SET NO COUNT 在 SP 中不存在 - 我添加了这个,它没有任何区别。 “您正在处理类型变量:表”是什么意思?
-
忽略我过去回答的这一部分。如果您添加了
SET NOCOUNT ON并且没有任何改变,我不知道有什么问题。
标签: sql-server excel vba ado