我正在尝试调试调用驻留在SQL Server(SQL Server 2008 R2)上的存储过程的Excel VBA代码。通过一些错误捕获,我现在可以看到错误是什么,但我不知道是什么导致它。
这是我的VBA代码:
Sub Add_Results_Of_ADO_Recordset()
Dim cnt As ADODB.Connection
Dim rst As ADODB.Recordset
Dim cmd As ADODB.Command
Dim stSQL As String
Const stADO As String = "Provider=SQLOLEDB.1;User ID =xxxxx;Password=xxxxx;" & _
"Persist Security Info=False;" & _
"Initial Catalog=MyDatabase;" & _
"Data Source=xxxxxxxxxxxx"
' initialise ADO
Set cnt = New ADODB.Connection
cnt.Open stADO
On Error GoTo Err_SaveProposal
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = cnt
.CommandText = "PolicyList"
.CommandType = adCmdStoredProc
.NamedParameters = True
.Parameters.Refresh
.Parameters.Append .CreateParameter("@Incept", adVarChar, adParamInput, 10, "2/1/2014")
.Parameters.Append .CreateParameter("@Expire", adVarChar, adParamInput, 10, "2/1/2014")
.Parameters.Append .CreateParameter("@PStatus", adVarChar, adParamInput, 20, "BOOK")
End With
' Debug code to ensure parameters are set correctly
For Each prm In cmd.Parameters
Debug.Print prm.Name & " : " & prm.Value
Next
cmd.Execute
If cnt.State = adStateOpen Then cnt.Close
Exit Sub
Err_SaveProposal:
Debug.Print Err.Number & ": " & Err.Description
If cnt.State = adStateOpen Then cnt.Close
End Sub
以下是写入即时窗口的消息:
@RETURN_VALUE :
@Incept :
@Expire :
@PStatus :
@Incept : 2/1/2014
@Expire : 2/1/2014
@PStatus : BOOK
-2147217900: Procedure or function PolicyList has too many arguments specified.
看起来我试图发送到存储过程的三个参数被发送两次。一旦作为空字符串,一个具有我想发送的值。有什么建议吗?