Error when Reusing ADO Connection & Command
- From: MarketWizard <MarketWizard@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 23 Oct 2006 09:53:02 -0700
Hello,
I have a loop where I am iterating through a list of values that I assign to
a parameter, then perform an execute. It works the first time thru the loop,
but fails with a 'missing expression' error the second pass. I checked the
command and recordset variables, and they look ok. Here is my code: A
solution wins you a prize, please help ASAP.
Sub Test2()
Dim strSQL, strConnectStr As String
Dim iCols As Integer
Dim strMsg As String
Dim GroupCode As String
Dim RunDate As String
Dim OrderBy As String
Dim customer, rownum, Col As Integer
Dim cn As New ADODB.Connection
Dim cmd2 As New ADODB.Command
Dim Rs2 As New ADODB.Recordset
Set customer_param = New ADODB.Parameter
Set rundate_param = New ADODB.Parameter
Set groupcode_param = New ADODB.Parameter
Set orderby_param = New ADODB.Parameter
'***********************************
' Connect to the Datasource
'***********************************
Set cn = GetNewConnection
cmd2.ActiveConnection = cn
' Get the input parameters from the excel work***
GroupCode = Worksheets("Main").Range("GroupCode")
RunDate = Worksheets("Main").Range("RunDate")
OrderBy = Worksheets("Main").Range("OrderBy")
'Set the target work***
Set Ws = Sheets("Main")
cmd2.CommandText = "iobu_issue_bet_matrix.get_data"
cmd2.CommandType = adCmdStoredProc
' Set the parameters for the stored procedure call
cmd2.Parameters.Append cmd2.CreateParameter("customer_param", adInteger,
adParamInput, , 1) ' just default parm val = 1, set actual val in loop below
cmd2.Parameters.Append cmd2.CreateParameter("groupcode_param", adVarChar,
adParamInput, 15, GroupCode)
cmd2.Parameters.Append cmd2.CreateParameter("rundate_param", adVarChar,
adParamInput, 10, RunDate)
cmd2.Parameters.Append cmd2.CreateParameter("orderby_param", adVarChar,
adParamInput, 25, OrderBy)
' Loop thru the customerS range
' Calling a stored procedure that will return a grid (ref cursor recordset)
to the spread*** for each
' time the stored procedure is called. These grids are concatenated
together to form a large continuous grid.
Dim r As Range
Dim row As Range
Set r = Ws.Range("customerS")
' Clear the work*** where we will put our results
Worksheets("Results").Cells.ClearContents
Rs2.CursorLocation = adUseClient
rownum = 1
Col = 1
'Set Ws
Set Ws = Sheets("Results")
For Each row In r.Rows
customer = row.Cells(1)
strMsg = "customer=" & customer
response = MsgBox(strMsg, vbOKOnly, "title", "DEMO.HLP", 1000)
If customer = 0 Then
Exit For
End If
' Set the parameter values
cmd2("customer_param") = customer
cmd2("groupcode_param") = GroupCode
cmd2("rundate_param") = RunDate
cmd2("orderby_param") = OrderBy
Set Rs2 = cmd2.execute
Ws.Range("A2").CopyFromRecordset Rs2
Next row
' Clean-Up
Rs2.Close
cn.Close
Set Rs2 = Nothing
Set cmd = Nothing
Exit Sub
ErrHandler:
'Clean-Up
If Rs2.State = adStateOpen Then
Rs2.Close
End If
If cn.State = adStateOpen Then
cn.Close
End If
Set Rs2 = Nothing
Set cn = Nothing
Set cmd2 = Nothing
If Err <> 0 Then
MsgBox Err.Source & "-->" & Err.Description, , "Error"
End If
End Sub
.
- Follow-Ups:
- Re: Error when Reusing ADO Connection & Command
- From: Bob Barrows [MVP]
- Re: Error when Reusing ADO Connection & Command
- From: Bob Barrows [MVP]
- Re: Error when Reusing ADO Connection & Command
- Prev by Date: Re: Stored procedure returning multiple recordsets
- Next by Date: Re: determine whether a field is identity column
- Previous by thread: Stored procedure returning multiple recordsets
- Next by thread: Re: Error when Reusing ADO Connection & Command
- Index(es):