Results 1 to 8 of 8

Thread: Exec stored procedure to return ADODB.Recordset [Resolved]

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Exec stored procedure to return ADODB.Recordset [Resolved]

    I created a stored procedure in SQL that outputs a record. I need
    to have a function in vb exec the sp and return a recordset to use
    in my program.
    Stored procedure...
    Code:
    IF EXISTS(SELECT Name FROM SysObjects
          WHERE Name = 'sp_RRGetTask' AND Type = 'P')
       DROP PROCEDURE sp_RRGetTask
    GO
    
    CREATE PROCEDURE sp_RRGetTask 
       @Task_No nvarchar(8) OUTPUT
    AS 
    SELECT *
       FROM Tasks 
       WHERE Task_No = @Task_No
    GO
    Function in vb to execute it.
    VB Code:
    1. Public Function Exec_SPrs(ByRef oCnnS As ADODB.Connection, ByVal sp_Name As String) As ADODB.Recordset
    2. '<RR 09/16/2003 - VB/OUTLOOK GURU>
    3.     On Error GoTo No_Bugs
    4.    
    5.     Dim oCM As ADODB.Command
    6.     Dim lRecs As Long
    7.    
    8.     Set oCM = New ADODB.Command
    9.     oCM.ActiveConnection = oCnnS
    10.     oCM.CommandType = adCmdStoredProc
    11.     oCM.CommandText = sp_Name
    12.    
    13.     'NEED TO CHANGE FROM HERE???
    14.     oCM.Execute lRecs
    15.     If lRecs > 0 Then
    16.         gbExec_SPrs = True
    17.     Else
    18.         gbExec_SPrs = False
    19.     End If
    20.     Exit Function
    21.    
    22. No_Bugs:
    23.    
    24.     MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbInformation, App.ProductName
    25.    
    26. End Function
    This is my code for executing stored procedures that don't return anything.
    How can I complete it to return an ADODB.Recordset?
    Last edited by RobDog888; Sep 17th, 2003 at 12:59 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  2. #2

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Updated function almost working. It is giving an "hstmt" error -
    "Connection is busy with results from another hstmt."
    Any ideas?
    I searched books on line and only came up with -
    The SQL Server ODBC driver allows only one active hstmt.
    Not much help.
    VB Code:
    1. Public Function Exec_SPrs(ByRef oCnnS As ADODB.Connection, ByVal sp_Name As String, Optional ByVal sParam As String) As ADODB.Recordset
    2. '<RR 09/16/2003 - VB/OUTLOOK GURU>
    3.     On Error GoTo No_Bugs
    4.    
    5.     Dim oRs As ADODB.Recordset
    6.     Dim oCM As ADODB.Command
    7.     Dim oParam As ADODB.Parameter
    8.     Dim lRecs As Long
    9.    
    10.     Set oRs = New ADODB.Recordset
    11.     Set oCM = New ADODB.Command
    12.     oCM.ActiveConnection = oCnnS
    13.     oCM.CommandType = adCmdStoredProc
    14.     oCM.CommandText = sp_Name
    15.     If sParam = "" Then
    16.         Set oParam = oCM.CreateParameter("Task_No", adChar, adParamInput, 8, sParam)
    17.         oCM.Parameters.Append oParam
    18.     End If
    19.     Set oRs = oCM.Execute(lRecs, sParam)
    20.     Set Exec_SPrs = oRs
    21.     Exit Function
    22.    
    23. No_Bugs:
    24.    
    25.     MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbInformation, App.ProductName
    26.    
    27. End Function
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    change Set oRs = oCM.Execute(lRecs, sParam) to Set oRs = oCM.Execute
    Also since you are passing back the recordset, you may want to disconnect it....
    VB Code:
    1. Set oRs = oCM.Execute
    2. Set oRS.ActiveConnection = Nothing
    3. Set Exec_SPrs = oRs
    4. Exit Function
    One last thing... you need to include the @ in the parameter name....
    VB Code:
    1. Set oParam = oCM.CreateParameter("@Task_No", adChar, adParamInput, 8, sParam)
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    If I do this - Set oRs = oCM.Execute - I get an error that its requires a parameter.
    I think I might of confused using a Command Object with
    executing a stored procedure. I get the original error when I try
    to perform any kind of operation on the same connection. So I will
    test your other option of .ActiveConnection. I will let you know
    how it goes.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    I missed it the first time, but now I see why it might be complaining about parameters...
    the ActiveConnection on the Command object needs to be SET.....
    oCM.ActiveConnection = oCnnS
    should be
    Set oCM.ActiveConnection = oCnnS

    As for the command object... that's what it's for, executing stored procedures....
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Ok. We got a little farther. The sp executes and no error
    requesting a parameter. I do get error stating that "Operation
    not allowed when object is open." It is refering to the line - Set
    oRs.ActiveConnection = Nothing.

    Also, I hade the logic backwards for the parameter - = instead of <>.
    I could create another connection just for executing the sp's.
    This is what is working.
    VB Code:
    1. Public Function Exec_SPrs(ByRef oCnnS As ADODB.Connection, ByVal sp_Name As String, Optional ByVal sParam As String) As ADODB.Recordset
    2. '<RR 09/16/2003 - VB/OUTLOOK GURU>
    3.     On Error GoTo No_Bugs
    4.    
    5.     Dim oRs As ADODB.Recordset
    6.     Dim oCM As ADODB.Command
    7.     Dim oParam As ADODB.Parameter
    8.     Dim lRecs As Long
    9.    
    10.     Set oRs = New ADODB.Recordset
    11.     Set oCM = New ADODB.Command
    12.     Set oCM.ActiveConnection = oCnnS
    13.     oCM.CommandType = adCmdStoredProc
    14.     oCM.CommandText = sp_Name
    15.     If sParam <> "" Then
    16.         Set oParam = oCM.CreateParameter("@Task_No", adChar, adParamInput, 8, sParam)
    17.         oCM.Parameters.Append oParam
    18.     End If
    19. '    Set oRs = oCM.Execute(lRecs, sParam)
    20.     Set oRs = oCM.Execute 'Works this way now
    21.     Set Exec_SPrs = oRs
    22.     Set oRs.ActiveConnection = Nothing 'Error here
    23.     Set oParam = Nothing
    24.     Set oCM = Nothing
    25.     Exit Function
    26.    
    27. No_Bugs:
    28.    
    29.     MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbInformation, App.ProductName
    30. '    Resume
    31. End Function
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Final version is working now. I had to create another connection
    and set it to be a server side cursor. The only thing is that it is
    not all that fast. I had converted it from a query to a sp.

    Any ideas on how to speed it up?
    VB Code:
    1. Public Function Exec_SPrs(ByRef oCnnS As ADODB.Connection, ByVal sp_Name As String, Optional ByVal sParam As String) As ADODB.Recordset
    2. '<RR 09/16/2003 - VB/OUTLOOK GURU>
    3.     On Error GoTo No_Bugs
    4.    
    5.     Dim oRs As ADODB.Recordset
    6.     Dim oCM As ADODB.Command
    7.     Dim oParam As ADODB.Parameter
    8.     Dim lRecs As Long
    9.    
    10.     Set oRs = New ADODB.Recordset
    11.     Set oCM = New ADODB.Command
    12.     Set oCM.ActiveConnection = oCnnS
    13.     oCM.CommandType = adCmdStoredProc
    14.     oCM.CommandText = sp_Name
    15.     If sParam <> "" Then
    16.         Set oParam = oCM.CreateParameter("@Task_No", adChar, adParamInput, 8, sParam)
    17.         oCM.Parameters.Append oParam
    18.     End If
    19.     Set oRs = oCM.Execute
    20.     Set Exec_SPrs = oRs
    21.     Set oCM.ActiveConnection = Nothing
    22.     Set oParam = Nothing
    23.     Set oCM = Nothing
    24.     Exit Function
    25.    
    26. No_Bugs:
    27.    
    28.     MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbInformation, App.ProductName
    29. '    Resume
    30. End Function
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Just some comments.

    1) Server side cursors bascially block any further activity on a connection. You can only have one server side recordset per connection.

    2) To disconnect a recordset you must use a client side cursor.

    3) You do not need to use the @ when defining a parameter name (unless you have NamedParameters option set).

    4) There is very little speed gain b/w executing a sql query vs a SP via the command object. Speed is gained when you execute the command more than once, say in a loop or something.

    5) Calling Set oCM.ActiveConnection = Nothing does not close the connection on the recordset.

    6) Using the syntax Set oRs = oCM.Execute there is no need to create an instance of the recordset beforehand.

    7) Also, when using the syntax Set oRs = oCM.Execute, a Read-Only Forward Only recordset is always created. Use the following syntax to control the cursor and lock types. Note in this case you need to create the recordset instance first.

    Set oRS = New ADODB.Recordset
    oRS.Open oCM, , adOpenStatic, adLockBatchOptimistic

    8) When executing a command that does not return a recordset use the following syntax, it is more efficient.

    oCM.Execute lRecs, , adExecuteNoRecords

    9) It probably does not matter in this case but generally you should define your command parameters to match the stored procedure parameters. The stored procedure you posted accepts an nvarchar(8) variable. Not a Char(8). Use the adVarWChar constant instead.

    10) HTH

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width