Results 1 to 5 of 5
  1. #1
    randman1 is offline Novice
    Windows XP Access 2003
    Join Date
    Dec 2009
    Posts
    25

    Difficulty returning ADODB recordset from function


    I'm trying to return a ADO recordset from a function that can be iterated, have records counted, etc. I can return the recordset if I do not close either the recordset or connection but know this to be bad practice as well as causing the MDB file to bloat. I've seen some solutions on the Net but none of them applied to Access VBA. An over simplified version of my code is as follows.

    Code:
    Public Function QueryServer(ByVal sSql as String) As ADODB.Recordset
        Dim cn as New ADODB.Connection
        Dim rs as New ADODB.Recordset
    
        cn.CursorLocation = adUseClient
        rs.Cursor = adUseClient
    
        cn.Open = "File Name=MyUDLFilePath"
        rs.Open sSql, cn, adOpenStatic
    
        Set QueryServer = rs
        cn.Close
    
    End Function
    
    
    Private Sub Command0_Click
        Dim sSql as String
        Dim rs as ADODB.Recordset
    
        sSql = "SELECT * FROM SomeTable"
        Set rs = QueryServer(sSql)
    
        Do While Not rs.EOF
            Debug.Print rs.Fields(0).Value
        Loop
    
        rs.Close
    
    End Sub

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    What is the advantage of using a function to open a record set, rather than opening the record set in the sub??? I would add the code necessary to open the record set to the sub.

  3. #3
    randman1 is offline Novice
    Windows XP Access 2003
    Join Date
    Dec 2009
    Posts
    25
    The primary advantage is that the function that returns the recordset does all of the error handling and is contained in a class that already has the other information necessary for connections. If the function was a simple as the above example, then I would agree that opening the recordset locally would be the thing to do.

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Interesting... I'll have to look into this. I use DAO, so opening a record set doesn't take a lot of code. But it is something to look into.

    Back to your problem, you need to close the record set, close the connection, set the record set to nothing and set the connection variable to nothing. Not sure how (timing) to do this when the record set is opened as a call to a function. Maybe someone else will have some ideas.

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 08-23-2012, 12:28 AM
  2. form/subform ADODB recordset
    By rashima in forum Forms
    Replies: 1
    Last Post: 04-27-2012, 12:19 PM
  3. Replies: 2
    Last Post: 03-08-2012, 12:59 PM
  4. ADODB Recordset behaving strangely
    By RayMilhon in forum Programming
    Replies: 4
    Last Post: 11-04-2011, 12:21 PM
  5. Recordset returning blank value
    By Mohamed in forum Access
    Replies: 1
    Last Post: 10-24-2011, 09:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums