Wednesday, 28 August 2013

Excel VBA executing SQL Server Stored Proc - Result set throwing Error 3704

Excel VBA executing SQL Server Stored Proc - Result set throwing Error 3704

I am trying to execute an SQL Server Stored Procedure from Excel VBA. The
Procedure returns rows into a Result Set object. However, while running
the code, it throws "3704 Operation is not allowed when the object is
closed" Error.
Note: There is no problem with the Database connection because Select
query run on the same Connection object are working fine.
Here is my code:
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

No comments:

Post a Comment