Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2010
    Posts
    44

    Unanswered: Proper way to do error checking in VBA for Access

    What is the code that is similar to a Try/Catch in Visual Basic 2008? I tried the below code, but it seems to always fire and show the message box even when there is no error message. Thanks.

    'On Error GoTo ErrHandler:

    ' sample code here


    'ErrHandler:
    ' MsgBox ("error has occured")
    ' Resume Next

  2. #2
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    You need an:

    Exit sub
    or
    Exit function

    after your code and before the

    ErrHandler:

    Otherwise your code continues on through the ErrHandler: section (and showing the msgbox "error has occured").

    ie.

    Exit sub
    ErrorHandler:
    or
    Exit Function
    ErrorHandler:
    Last edited by pkstormy; 03-27-10 at 10:06.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  3. #3
    Join Date
    Jan 2010
    Posts
    44
    I add a message box to a button click like you said and get the error below

    Private Sub btnUpdate_Click()
    On Error GoTo ErrHandler:

    MsgBox ("test")


    End Sub
    ErrHandler:
    MsgBox ("error has occured")


    Error:
    ---------------------------
    Microsoft Visual Basic
    ---------------------------
    Compile error:

    Label not defined
    ---------------------------
    OK Help
    ---------------------------

  4. #4
    Join Date
    Jul 2004
    Location
    South Dakota
    Posts
    267
    You need to use Exit Sub and not End Sub...like this.

    Code:
    Private Sub btnUpdate_Click()
         On Error GoTo ErrHandler:
    
         MsgBox ("test")
    
    
         Exit Sub
    
    ErrHandler:
         MsgBox ("error has occured")
    
    End Sub

  5. #5
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    Typically there's also an exit handler. Here's a little example:

    Error trapping
    Paul

  6. #6
    Join Date
    Jan 2010
    Posts
    44
    That was the issue. Thanks for the help and examples.

Posting Permissions

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