Results 1 to 5 of 5
  1. #1
    5 Star Lounger
    Join Date
    Jan 2004
    Location
    Norfolk, England
    Posts
    744
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Error Handling - General question (2000)

    Hi,
    Is there a general way to handle Access errors?
    I'd like to replace various Access standardised errors with my own simplified version MsgBoxes. So for instance the message below, I would like to simplify and remove the Access error so I don't get two errors for the same process.
    But is there also a general way of doing this for all errors, or do the error numbers need to be found?

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

    Re: Error Handling - General question (2000)

    The message box in your screenshot is not an error message, it is a warning. So error handling will not affect it. Instead, you need to write an event procedure for the appropriate event, in this case the On Delete event or the Before Del Confirm event of the form. For example:

    Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)
    Cancel = (MsgBox("Delete this record?", vbOKCancel) = vbCancel)
    ' Suppress built-in warning
    Response = acDataErrContinue
    End Sub

    You can create a general error handler to some extent in forms, by creating an event procedure for the On Error event of the form. At its most basic, you could display a single error message for all errors:

    Private Sub Form_Error(DataErr As Integer, Response As Integer)
    MsgBox "You stupid fool!", vbExclamation
    ' Suppress built-in error message
    Response = acDataErrContinue
    End Sub

    The MsgBox is generic, it does not give the user a clue what went wrong. If you want to display a message specific to the error that occurred, you must inspect the error number in the DataErr argument. It is up to you as the programmer to find out which error numbers can occur, and to decide how to handle them.

    Private Sub Form_Error(DataErr As Integer, Response As Integer)
    Select Case DataErr
    Case 2501
    ' Action cancelled - ignore
    Case 2105
    ' Cannot go to record
    MsgBox "You can't go there!", vbInformation
    Case Else
    ' All other errors - generic error message
    MsgBox "Something went wrong!", vbInformation
    End Select
    ' Suppress built-in error message
    Response = acDataErrContinue
    End Sub

  3. #3
    5 Star Lounger
    Join Date
    Jan 2004
    Location
    Norfolk, England
    Posts
    744
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Error Handling - General question (2000)

    Thanks, this explains alot.
    I presume the
    Select Case DataErr
    Case 2501

    with Case 2501 is the error number produced by Access. I'm also guessing that the easiest way to find the error number is to produce the function that displays the error.

    This will allow me to control things to a greater degree with the design of a database.

  4. #4
    5 Star Lounger
    Join Date
    Jan 2004
    Location
    Norfolk, England
    Posts
    744
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Error Handling - General question (2000)

    Thanks Hans, this is really helpful. I shall now go and do some fiddling...................... <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>

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

    Re: Error Handling - General question (2000)

    To find out which errors/error numbers occur, use code like this during development/testing:

    Private Sub Form_Error(DataErr As Integer, Response As Integer)
    MsgBox "Error number " & DataErr & ": " & AccessError(DataErr)
    ' Suppress built-in error message
    Response = acDataErrContinue
    End Sub

    This will display both the error number and the corresponding error message. If an error occurs during testing, write down the error number and its cause. When you have a good idea of which errors can occur, use the error numbers in a Select Case statement.

Posting Permissions

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