2014-02-19 82 views
0

我有一個簡單的VBA代碼,需要使用一個參數從SQL Server執行SP。我複製了一本教科書中的代碼,進行了相關的調整,但是我一直收到這個錯誤。 下面是代碼:VBA SQL Server參數類型錯誤

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

聲明一個rsData變量,但你初始化rsDate。你不是在使用Option Explicit聲明嗎?

+0

魔鬼是在細節。非常感謝! – user2343837