Click here to Skip to main content
15,921,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Experts,

C#
Public Sub SQLPrepareCmd()
  
  'Declare the params
 Dim prmBS1 As ADODB.Parameter
 Dim prmBS2 As ADODB.Parameter
 
 Set oRst = New ADODB.Recordset
  
    Set oCmd = New ADODB.Command
    oCmd.ActiveConnection = oCon
    oCmd.CommandType = adCmdStoredProc
    oCmd.CommandText = "spTest"
    oCmd.CommandTimeout = 0

' Set up the parameter for the Stored Procedure

  oCmd.Parameters.Append oCmd.CreateParameter("Periode1", adVarChar, adParamInput, 10, Range("Q3").Text)
  oCmd.Parameters.Append oCmd.CreateParameter("Periode2", adVarChar, adParamInput, 10, Range("Q4").Text)
    
 
    Set oRst = oCmd.Execute(, , adCmdStoredProc)
   
    Application.StatusBar = "Running stored procedure..."

If oRst.EOF = False Then
        ActiveSheet.Cells(40, 2).CopyFromRecordset oRst
            oRst.Close
            
        oCon.Close
             
        Set oCon = Nothing
        Set oCmd = Nothing

End Sub


I am getting this msg: Runtime Error 3704:
Operation is not allowed when the object is closed

on this line
If oRst.EOF = False Then


Will be glad for a help. I tried something like this but it isn't working
VB
'* Open recordset
   Set oRst.Source = oCmd
   oRst.Open oCmd
Posted
Updated 8-Dec-15 22:47pm
v3

1 solution

You are using oCmd before it has been initialised. Try
C#
  Set oCmd = New ADODB.Command
  oCmd.ActiveConnection = oCon
  oCmd.CommandType = adCmdStoredProc
  oCmd.CommandText = "spTest"
  oCmd.CommandTimeout = 0

oCmd.Parameters.Append oCmd.CreateParameter("Periode1", adVarChar, adParamInput, 10, Range("Q3").Text)
oCmd.Parameters.Append oCmd.CreateParameter("Periode2", adVarChar, adParamInput, 10, Range("Q4").Text)
 
Share this answer
 
Comments
ZurdoDev 8-Dec-15 10:24am    
Maybe I'm blind, but the OP does have the init code for oCmd.
CHill60 8-Dec-15 10:31am    
Yes they do, but it appears after they have attempted to append to parameters (of the then empty object) - hence I moved the Parameters.Append to after the Set oCmd = New ADODB.Command.
ZurdoDev 8-Dec-15 10:37am    
Duh. Yes, it is very clear. It's official, I am blind.

I'll scurry back to work now.

+5
CHill60 8-Dec-15 10:40am    
*chuckle*

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