Results 1 to 4 of 4
  1. #1
    Aaron5714 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    48

    Error Handling with ADODB Recordset Object

    I have a few procedures that write records to a table, such as the example below which runs when the database starts and writes to a user log table. From what I understand, when you open a recordset object it is always important to close it and set it to "nothing" before the procedure ends.

    I am now adding some basic error handling to all the code modules in my database. In the example below, should the two lines under "clean up" be placed after the "ExitHere:" line? I would assume that if an error occurs when trying to write to the table, we would still want to close the recordset after the error handler message appears. However, if I place the clean up statements after "ExitHere:" and an error is triggered before the recordset is created or opened, would that then cause an error when I try to close the non-existent recordset?
    Code:
    Private Sub Form_Open(Cancel As Integer)
    
        On Error GoTo ErrHandler
    
        'hide startup form
        Me.Visible = False
        
        'maximize application window and minimize ribbon
        DoCmd.RunCommand acCmdAppMaximize
        CommandBars.ExecuteMso "MinimizeRibbon"
            
        'get version #
        Dim VersionNum As String
        VersionNum = DLookup("Version", "tblVersion")
        
        'setup and open UserLog recordset
        Dim UserLogRS As New ADODB.Recordset
        UserLogRS.Open "tblUserLog", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
        
        'Add new record to UserLog
        With UserLogRS
            .AddNew
            ![UserID] = (Environ$("Username"))
            ![LoginTime] = Now()
            ![Version] = VersionNum
            .Update
        End With
        
        'clean up
        UserLogRS.Close
        Set UserLogRS = Nothing
    
    ExitHere:
        DoCmd.Close acForm, "frmStartUp", acSaveNo
        DoCmd.OpenForm "frmMain"
        Exit Sub
    
    ErrHandler:
        MsgBox "An unexpected error occurred. If this problem persists please contact the system administrator and reference the following information:" _
            & vbNewLine & vbNewLine & "Error Number: " & Err.Number & vbNewLine & "Description: " & Err.Description, _
            vbCritical, "VBA Error"
        Resume ExitHere
    End Sub


  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Yes, I would have them in the exit handler. You can handle the possibility of an error there by starting the exit handler with

    On Error Resume Next
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Aaron5714 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    48
    Makes sense... thanks for the quick reply Paul!

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problemo!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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. Difficulty returning ADODB recordset from function
    By randman1 in forum Programming
    Replies: 4
    Last Post: 07-19-2012, 01:07 PM
  3. form/subform ADODB recordset
    By rashima in forum Forms
    Replies: 1
    Last Post: 04-27-2012, 12:19 PM
  4. Replies: 2
    Last Post: 03-08-2012, 12:59 PM
  5. ADODB Recordset behaving strangely
    By RayMilhon in forum Programming
    Replies: 4
    Last Post: 11-04-2011, 12:21 PM

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