Results 1 to 7 of 7
  1. #1
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Error Trapping. (A2000)

    I skip over my errors in the following way:-

    On Error Go To Errtrap

    "Whatever comands On Click, or, After Update etc etc etc "

    Exit Sub
    ErrTrap:

    I apply this when I know a control is working ok, But.
    In my Run-Time version, if I hit and error, I get kicked out of the system. I know this a feature of Run Time.

    I need to insert a message box telling why I have hit an Error so I can resolve it before distribution.


    Dave

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

    Re: Error Trapping. (A2000)

    <P ID="edit" class=small>Edited by HansV on 08-Aug-02 16:42.</P>Oops - mistake in error section corrected

    Dave,

    My routines look something like this. The dbs and rst are just for illustration purposes; I include them because they need to be set to Nothing whether the procedure ends normally or not. In the error trap section, I display the error number (so you can look it up) and description, then resume to the exit section, where the necessary clean up is done. I silently ignore error 2501 - you get this for instance when you cancel the opening of a report because there are no data.

    Sub SomethingOrOther()

    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset

    On Error Goto ErrTrap

    ' Commands go here, e.g.
    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset("tblSomething")
    ' etc.

    ExitHere:

    ' Clean up after you; make sure you don't get an error here.
    On Error Resume Next
    rst.Close
    Set rst = Nothing
    Set dbs = Nothing
    ' OK, now get out of here
    Exit Sub

    ErrTrap:

    ' Error 2501 occurs when something is canceled.
    ' This is usually not a real error, so no message.
    If Err = 2501 Then
    Resume Next
    Else
    MsgBox "Error " & Err & " occurred, with description: " & vbCrLf & Err.Description, vbExclamation
    'Always clean up!
    Resume ExitHere
    End If

    End Sub

  3. #3
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Error Trapping. (A2000)

    Hans.

    Just to make sure before I copy and paste throughout my db.

    Mine should look like the following.

    On Error Goto Errtrap

    "?????????????? click etc"

    Exit Sub
    Errtrap:
    If Err = 2501 Then
    Resume Next
    Else
    MsgBox "Error " & Err & " occurred, with description: " & vbCrLf & Err.Description, vbExclamation
    'Always clean up!
    Resume ExitHere
    End If

    .................................................. ...........

    The part that says (If Err = 2501) should it be (If Errtrap = 2501)

    and the part that says (Resume ExitHere) should it be (Resume 'ExitHere)

    Thanks

    Dave

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

    Re: Error Trapping. (A2000)

    Hello Dave,

    I'll comment the highlighted parts below.

    <span style="background-color: #FFFF00; color: #000000; font-weight: bold">ExitHere:</span hi>

    ' cleanup commands go here
    Exit Sub

    Errtrap:

    If <span style="background-color: #FFFF00; color: #000000; font-weight: bold">Err</span hi> = 2501 Then
    Resume Next
    Else
    MsgBox "Error " & <span style="background-color: #FFFF00; color: #000000; font-weight: bold">Err</span hi> & " occurred, with description: " & vbCrLf & Err.Description, vbExclamation
    'Always clean up!
    <span style="background-color: #FFFF00; color: #000000; font-weight: bold">Resume ExitHere</span hi>
    End If

    (1) <font color=blue>ExitHere:</font color=blue> is a label. When an error (other than 2501) occurs, an error message is displayed, then the code resumes at the label ExitHere. This makes it possible to execute any necessary rounding up that *must* be done, regardless of whether an error occurred or not. If you don't need any rounding up, you might omit the <font color=blue>ExitHere:</font color=blue> label and the <font color=blue>Resume ExitHere</font color=blue> statement.

    (2) <font color=blue>Err<font color=blue> is an object that contains information on the error that occurred. This object has several properties, of which Number and Description are the most important in this situation. Number is the default property; if you just refer to Err, it is assumed that you mean Err.Number. So instead of

    If Err = 2501 Then

    you can also write

    If Err.Number = 2501 Then

    In the MsgBox instruction, you can also use

    ... "Error " & Err.Number & " occurred, with description: " ...

    instead of

    ... "Error " & Err & " occurred, with description: " ...

    Regards,
    Hans

  5. #5
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Error Trapping. (A2000)

    So I can Use:-

    On error goto errtrap

    "Click etc etc"

    Exit Sub
    Errtrap:
    If Err = 2501 Then
    Resume Next
    Else
    MsgBox "Error " & Err & " occurred, with description: " & vbCrLf & Err.Description, vbExclamation
    'Always clean up!
    Resume
    End If


    Sorry to be so critical with this.
    I have quite alot of Errtraps) in my db and before I set out to alter them, I want to make sure I have the correct syntax.

    I understand the Exit Here now but don't think I really need them.
    As Long as I have a msg popup when I hit the error, I can pursue to correct them. (Apart from 2501 of course)

    Thanks
    Dave

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

    Re: Error Trapping. (A2000)

    Hi Dave,

    Almost there. since you decided not to use the cleanup part, you only need to display the error message in the error handling part. The Resume you left after it is not necessary.

    Regards,
    Hans

    <img src=/w3timages/blueline.gif width=33% height=2>

    On Error GoTo ErrTrap

    "Click etc etc"

    Exit Sub
    ErrTrap:
    If Err = 2501 Then
    Resume Next
    Else
    MsgBox "Error " & Err & " occurred, with description: " & vbCrLf & Err.Description, vbExclamation
    End If

  7. #7
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Error Trapping. (A2000)

    Thanks Hans.

    This has done exactly what I expected.
    I had a couple of problems in my db.
    When selecting a form from a custom built menu, The db would shut down.

    This has now highlited the problems.

    I'm getting Error 2450 can't find form Whatever.

    I have something to go by now.

    Dave

Posting Permissions

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