【发布时间】:2017-01-03 05:20:58
【问题描述】:
使用 SQL Server 2012 \ Excel 2010。Excel 调用 SQL SP,它将返回 1 或 2,然后我需要基于此执行其他操作。
Dim con As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.RecordSet
Dim WSP1 As Worksheet
Set con = New ADODB.Connection
Set cmd = New ADODB.Command
Set rs = New ADODB.RecordSet
' Log into our SQL Server, and run the Stored Procedure
con.Open "Provider=SQLOLEDB;Data Source=xxxxxxxx;Initial Catalog=xxxxxxx;Integrated Security=SSPI;Trusted_Connection=Yes;"
cmd.ActiveConnection = con
' Set up the parameter for our Stored Procedure
' (Parameter types can be adVarChar,adDate,adInteger)
cmd.Parameters.Append cmd.CreateParameter("User", adVarChar, adParamInput, 15, Trim(Range("C7").Text))
Application.StatusBar = "Running stored procedure..."
cmd.CommandText = "usp_GL_Code_Access"
Set rs = cmd.Execute(, , adCmdStoredProc)
If rs = 1 Then
MsgBox "true"
Else
MsgBox "false"
rs.Close
Set rs = Nothing
Set cmd = Nothing
con.Close
Set con = Nothing
目前我只是使用 msgbox true\false 来查看来自 rs\SQL SP 的值,但我不断收到错误消息“类型不匹配”并且它突出显示了
IF rs = 1 Then
线。
有什么想法吗?
【问题讨论】:
标签: sql-server vba excel