VBA SQL Server参数类型错误

时间:2014-02-19 22:43:08

标签: sql-server vba excel-vba excel

我有一个简单的VBA代码,需要使用一个参数从SQL Server执行SP。我从教科书中复制了代码,进行了相关调整,但我一直收到这个错误。 这是代码:

Public Sub ExecuteStoredProcAsMethod()
Dim objConn As ADODB.Connection
Dim rsData As ADODB.Recordset
Dim sConnect As String

'Create the conntection string.
sConnect = "Provider=SQLOLEDB;Data Source=MYSERVER; " & _
"Initial Catalog=DisaggregatedPatronage;Integrated Security=SSPI"

'Create the connection and Recordset objects.
Set objConn = New ADODB.Connection
Set rsDate = New ADODB.Recordset

'Open the connection and execute the SP.
objConn.Open sConnect
objConn.spSurveyDataTest "Bus", rsData

'Make sure we got records back
If Not rsData.EOF Then
    'Dump the contens of the recodset on the worksheet
    Sheet1.Range("A1").CopyFromRecordset rsDate
    'Close the recordset
    rsData.Close
    'Fit the column wirdths to the data
    Sheet1.UsedRange.EntireColumn.AutoFit
Else
    MsgBox "Error: No records returned. ", vbCritical
End If

'Clean up our ADO objects.
If CBool(objConn.State And adStateOpen) Then objConn.Close
Set objConn = Nothing
Set rsData = Nothing


End Sub

1 个答案:

答案 0 :(得分:1)

您声明了rsData变量,但您初始化rsDate。你不是在使用Option Explicit声明吗?