Results 1 to 2 of 2
  1. #1
    4 Star Lounger
    Join Date
    Jun 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    527
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Proper use of Record Sets (DAO) (A2003)

    Hello All,
    I am using DAO to open selected record sets in some tables. What is the proper method of making sure those recordsets are closed after use? I have heard that some of these can be persistent unless properly closed.

    Thanks,
    Mark

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Proper use of Record Sets (DAO) (A2003)

    Always include error handling in your code, and close the record set in the exit section. A general setup could look like this:

    Sub ThisOrThat()
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset

    On Error GoTo ErrHandler

    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset("tblSomething", dbOpenDynaset)

    ' Code to do something with the recordset goes here
    ...

    ExitHandler:
    On Error Resume Next
    rst.Close
    Set rst = Nothing
    Set dbs = Nothing
    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub

    If an error occurs, the error message is displayed, then the exit section is executed; this closes the recordset and sets the object variables to Nothing.

Posting Permissions

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