Results 1 to 3 of 3
  1. #1
    2 Star Lounger
    Join Date
    Dec 2000
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Nothing without Close (A2K)

    In A2K, using ADO:

    Are there any potential problems setting a recordset to Nothing *without* specifically closing it? I would only do this if there was an error.

    *** Start Code ***

    Private Sub DoMe ()

    Dim rst As ADODB.Recordset
    On Error GoTo ErrHandler
    Set rst = New ADODB.Recordset

    rst.Open [arguments etc]

    [normal processing code]

    rst.Close

    SubExit:
    set rst = Nothing
    On Error GoTo 0
    Exit Sub

    ErrHandler:
    [error processing code]
    Resume SubExit

    End Sub

    *** End Code ***

    In this case, if there was an error in the "Normal processing", rst would be 'Nothing'd' without being 'Closed'. Anything I should be aware of ?

    TIA
    Donald

  2. #2
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,624
    Thanks
    3
    Thanked 60 Times in 60 Posts

    Re: Nothing without Close (A2K)

    It seems to me that certain logic errors could cause a record to not be saved if you encounter an error and don't use the Close statement. To resolve the issue of possibly closing the recordset and generating another error because it never opened successfully, I would add the following lines to your exit procedure:

    SubExit:
    On Error Resume Next
    rst.Close
    set rst=Nothing
    On Error GoTo 0
    Exit Sub

    Hope this helps.
    Wendell

  3. #3
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Nothing without Close (A2K)

    I use WendellB's approach. Even though you *can* use Set Nothing in ADO without closing the recordset, it isn't a good habit to get into if you also work in DAO, as most of us do. Specifically closing the object doesn't hurt in ADO and makes it obvious in your code what your intentions are. In DAO, it's critical, so it isn't a bad idea to be consistent and use it in both object models.
    Charlotte

Posting Permissions

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