Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Return ADO Recordset from Function
Message
General information
Forum:
Visual Basic
Category:
Database DAO/RDO/ODBC/ADO
Miscellaneous
Thread ID:
00596301
Message ID:
00596346
Views:
17
Jason;

Take a look at this code - it might help you.

Some code fragments:
Option Explicit

'Declare database objects
Private objConnPublisher As ADODB.Connection
Private objRSPublisher As ADODB.Recordset
Dim strPublisher As String

Private Sub cmdSearch_Click()
  'Set a reference to the ADO connection object
   Set objConnSearch = New ADODB.Connection
   Set objRSSearch = New ADODB.Recordset


' Add code to allow user selection...
strPublisher = Trim(cboPublisher)


If Len(cboPublisher.Text) = 0  Then
    ' No user input...
    strSearch = ""
Else
' Publisher...
    strSearch = " Where Pub_Name = '" & strPublisher & "' Order By City "
End If


' Minimize connection time...
objConnSearch.Open "Driver=SQL Server;Server=T22DEV18;" & _
                "Database=Pubs"


objRSSearch.Source = _
     "SELECT pub_name, City, Country  FROM Publishers " & _
        " " & strSearch & "  "
     
objRSSearch.Open , objConnSearch, 1, 1

If objRSSearch.RecordCount > 0 Then
‘ Assign values to flex grid…
    MSHFlexGrid1.Refresh

    MSHFlexGrid1.CollapseAll
' Set up grid...
    MSHFlexGrid1.ColWidth(0) = 2000
    MSHFlexGrid1.ColWidth(1) = 2000
    MSHFlexGrid1.ColWidth(2) = 2000

    MSHFlexGrid1.ColHeaderCaption(0, 0) = "Publication"
    MSHFlexGrid1.ColHeaderCaption(0, 1) = "City"
    MSHFlexGrid1.ColHeaderCaption(0, 2) = "Country"

Set MSHFlexGrid1.DataSource = objRSSearch

' Clean up...
    objRSSearch.Close
    Set objRSSearch = Nothing
    
    objConnSearch.Close
    Set objConnSearch = Nothing
Else
    MsgBox "No records found"
End If

End Sub
>I have a funcion in class module that returns an ADODB.Recordset. I understand the theory behind this. My problem is that I do not know how to return the recordset to the calling sub. Here is the inf:
>
>Public Function GetRS(SQL As String) As ADODB.Recordset
> 'SET THE CONNECTION STRING PROPERTY TO A VALID CONNECTION STRING
> 'PASS AN SQL STATEMENT TO THIS FUNCTION
> 'THE RETURN VALUE WILL BE AN ADODB RECORDSET
>
> Dim rs As New ADODB.Recordset
> On Error GoTo LocalError
> With rs
> .ActiveConnection = ConnectionString
> .CursorLocation = adUseClient
> .LockType = adLockOptimistic
> .CursorType = adOpenKeyset
> .Source = SQL
> .Open
> Set .ActiveConnection = Nothing
> End With
> Set GetRS = rs
> Set rs = Nothing
>Exit Function
>LocalError:
> m_sLastError = Err.Number & " - " & Err.Description
> Set rs = Nothing
>End Function
>
>What do I need to do in the calling function to return the recordset? I tried to declare a recordset and do something like:
>
>rst = GetRS(strSQL)
>
>This is not work. Said invalid use of property or something.
>Anyone have some help?
Previous
Reply
Map
View

Click here to load this message in the networking platform