|
-
Sep 16th, 2003, 11:59 PM
#1
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:
Public Function Exec_SPrs(ByRef oCnnS As ADODB.Connection, ByVal sp_Name As String) As ADODB.Recordset
'<RR 09/16/2003 - VB/OUTLOOK GURU>
On Error GoTo No_Bugs
Dim oCM As ADODB.Command
Dim lRecs As Long
Set oCM = New ADODB.Command
oCM.ActiveConnection = oCnnS
oCM.CommandType = adCmdStoredProc
oCM.CommandText = sp_Name
'NEED TO CHANGE FROM HERE???
oCM.Execute lRecs
If lRecs > 0 Then
gbExec_SPrs = True
Else
gbExec_SPrs = False
End If
Exit Function
No_Bugs:
MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbInformation, App.ProductName
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Sep 17th, 2003, 11:45 AM
#2
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:
Public Function Exec_SPrs(ByRef oCnnS As ADODB.Connection, ByVal sp_Name As String, Optional ByVal sParam As String) As ADODB.Recordset
'<RR 09/16/2003 - VB/OUTLOOK GURU>
On Error GoTo No_Bugs
Dim oRs As ADODB.Recordset
Dim oCM As ADODB.Command
Dim oParam As ADODB.Parameter
Dim lRecs As Long
Set oRs = New ADODB.Recordset
Set oCM = New ADODB.Command
oCM.ActiveConnection = oCnnS
oCM.CommandType = adCmdStoredProc
oCM.CommandText = sp_Name
If sParam = "" Then
Set oParam = oCM.CreateParameter("Task_No", adChar, adParamInput, 8, sParam)
oCM.Parameters.Append oParam
End If
Set oRs = oCM.Execute(lRecs, sParam)
Set Exec_SPrs = oRs
Exit Function
No_Bugs:
MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbInformation, App.ProductName
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Sep 17th, 2003, 12:23 PM
#3
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:
Set oRs = oCM.Execute
Set oRS.ActiveConnection = Nothing
Set Exec_SPrs = oRs
Exit Function
One last thing... you need to include the @ in the parameter name....
VB Code:
Set oParam = oCM.CreateParameter("@Task_No", adChar, adParamInput, 8, sParam)
-
Sep 17th, 2003, 12:29 PM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Sep 17th, 2003, 12:33 PM
#5
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....
-
Sep 17th, 2003, 12:44 PM
#6
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:
Public Function Exec_SPrs(ByRef oCnnS As ADODB.Connection, ByVal sp_Name As String, Optional ByVal sParam As String) As ADODB.Recordset
'<RR 09/16/2003 - VB/OUTLOOK GURU>
On Error GoTo No_Bugs
Dim oRs As ADODB.Recordset
Dim oCM As ADODB.Command
Dim oParam As ADODB.Parameter
Dim lRecs As Long
Set oRs = New ADODB.Recordset
Set oCM = New ADODB.Command
Set oCM.ActiveConnection = oCnnS
oCM.CommandType = adCmdStoredProc
oCM.CommandText = sp_Name
If sParam <> "" Then
Set oParam = oCM.CreateParameter("@Task_No", adChar, adParamInput, 8, sParam)
oCM.Parameters.Append oParam
End If
' Set oRs = oCM.Execute(lRecs, sParam)
Set oRs = oCM.Execute 'Works this way now
Set Exec_SPrs = oRs
Set oRs.ActiveConnection = Nothing 'Error here
Set oParam = Nothing
Set oCM = Nothing
Exit Function
No_Bugs:
MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbInformation, App.ProductName
' Resume
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Sep 17th, 2003, 12:57 PM
#7
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:
Public Function Exec_SPrs(ByRef oCnnS As ADODB.Connection, ByVal sp_Name As String, Optional ByVal sParam As String) As ADODB.Recordset
'<RR 09/16/2003 - VB/OUTLOOK GURU>
On Error GoTo No_Bugs
Dim oRs As ADODB.Recordset
Dim oCM As ADODB.Command
Dim oParam As ADODB.Parameter
Dim lRecs As Long
Set oRs = New ADODB.Recordset
Set oCM = New ADODB.Command
Set oCM.ActiveConnection = oCnnS
oCM.CommandType = adCmdStoredProc
oCM.CommandText = sp_Name
If sParam <> "" Then
Set oParam = oCM.CreateParameter("@Task_No", adChar, adParamInput, 8, sParam)
oCM.Parameters.Append oParam
End If
Set oRs = oCM.Execute
Set Exec_SPrs = oRs
Set oCM.ActiveConnection = Nothing
Set oParam = Nothing
Set oCM = Nothing
Exit Function
No_Bugs:
MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbInformation, App.ProductName
' Resume
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Sep 17th, 2003, 03:55 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|