Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a vba to read record from SQL server.
and major code as below
VB
  Set Conn = New ADODB.Connection
  Set rs = New ADODB.Recordset
  Conn.CursorLocation = adUseServer
  Conn.Open ConStr
  Set cmd = New ADODB.Command
  cmd.CommandText = StoredProcedureName
  cmd.CommandType = adCmdStoredProc
  cmd.ActiveConnection = Conn
  With rs
    Set .ActiveConnection = Conn
    .LockType = adLockOptimistic
    .CursorLocation = adUseServer
    .CursorType = adOpenForwardOnly
    .Open "SET NOCOUNT ON"
  End With

cmd.Parameters("@regisno").Value = "ABC"
cmd.Parameters("@finid").Value = "AF123"
cmd.Parameters("@Month").Value = "2020-09-01"
rs.Open cmd, , , , adCmdStoredProc
If rs.EOF Then
Else
End if
and error message as below
"Operation is not allowed when the object is closed"
program are stop at if rs.eof then

What I have tried:

I'd tried various of @finid some are working fine but some are fail to get record.
even though my SQL program also add in "SET NOCOUNT ON" at initial but result still same.

Your kind advice are many appreciated.
yungu
Posted
Updated 17-Sep-20 23:55pm
v2
Comments
Sandeep Mewara 18-Sep-20 6:03am    
Sometimes, Warnings may confuse in the result. Try: SET ANSI_WARNINGS OFF

1 solution

You're calling Open on the Recordset twice, which probably isn't helping.

You should also make sure you're not sharing the connection, command, or recordset variables between multiple methods.

Try something like this:
VB
Set Conn = New ADODB.Connection
Conn.CursorLocation = adUseServer
Conn.Open ConStr

Set cmd = New ADODB.Command
cmd.CommandText = StoredProcedureName
cmd.CommandType = adCmdStoredProc
cmd.ActiveConnection = Conn

cmd.Parameters("@regisno").Value = "ABC"
cmd.Parameters("@finid").Value = "AF123"
cmd.Parameters("@Month").Value = "2020-09-01"

Set rs = New ADODB.Recordset
With rs
    .LockType = adLockOptimistic
    .CursorLocation = adUseServer
    .CursorType = adOpenForwardOnly
End With

rs.Open cmd, , , , adCmdStoredProc
How To Open ADO Connection and Recordset Objects[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900