我想從Excel VBA執行SQL Server存儲過程。該過程將行返回到結果集對象中。然而,運行代碼時,它拋出一個錯誤:Excel VBA執行SQL Server存儲過程 - 結果集拋出錯誤3704
3704 Operation is not allowed when the object is closed
注:
有有因爲Select
查詢相同的連接對象上運行工作正常數據庫連接沒有問題。
這裏是我的代碼:
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim cmd As ADODB.Command
Dim prm As ADODB.Parameter
Dim rst As New ADODB.Recordset
Set cn = New ADODB.Connection
Set cmd = New ADODB.Command
ThisWorkbook.initialize
cn.Provider = "sqloledb"
cn.Properties("Data Source").Value = ThisWorkbook.server
cn.Properties("Initial Catalog").Value = ThisWorkbook.db
cn.Properties("User ID").Value = "xxxxx"
cn.Properties("Password").Value = "xxxxx"
cn.Open
Set cmd = New ADODB.Command
cmd.CommandText = "Generate_KPI_Process_Quality_Check_RunTime"
cmd.CommandType = adCmdStoredProc
cmd.ActiveConnection = cn
Set prm = cmd.CreateParameter("@currentMonth", adChar, adParamInput, 255, cmb_month.Value)
cmd.Parameters.Append prm
Set prm = cmd.CreateParameter("@center", adChar, adParamInput, 255, cmb_center.Value)
cmd.Parameters.Append prm
rst.CursorType = adOpenStatic
rst.CursorLocation = adUseClient
rst.CursorLocation = adUseServer
rst.LockType = adLockOptimistic
rst.Open cmd
If (rst.BOF And rst.EOF) Then
'Some Code
End If
在哪一行它拋出這個錯誤? – jerussell
Hi @jerussell,它會在代碼段末尾的If(rst.BOF和rst.EOF)Then ... **語句處引發錯誤。 –
@Abhishek熊貓有沒有解決您的問題?如果是這樣,upvote和/或標記爲已回答。 – Tarik