Custom Error Messages

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #1

    Custom Error Messages

    One of the most frequently asked questions here at TheScripts is: Can I replace Standard Access Error Messages with my own? The answer is yes under certain circumstances and it involves the placement of code in a Form's Error() Event and setting a specific Parameter to either 1 of 2 values. Many Errors occur while your Form is active and it is within this context that you can replace the standard Access behavior when these Errors occur with behavior that is a little friendlier to the User. As previously stated, by attaching code to the Error() Event of a Form, your Procedure will be called whenever a Trappable Error occurs while the Form is running.

    The Syntax for the call is as follows:
    Sub Form_Error(Data Err As Integer, Response As Integer)
    __1. DataErr - contains the Error Number for the Error that just occurred.
    __2. Response - Allows you to specify how you want Access to handle the Error. If your code handles the Error to your satisfaction and you don't want Access to intervene or display its own message, place the value acDataErrContin ue in Response. If you want Acces to display its own Error Message, place acDataErrDispla y in Response.

    The following code traps 4 Errors that might pop up. In each case, the procedure replaces the Standard Access Error Message with its own. If an Error occurs that it hadn't planned on, the Procedure just passes the responsibility back to Access:
    Code:
    Private Sub Form_Error(DataErr As Integer, Response As Integer)
    Const conErrDataValidation = 3317
    Const conErrDataType = 2113
    Const conErrDuplicateKey = 3022
    Const conErrNullKey = 3058
    
    Select Case DataErr
       Case conErrDataValidation, conErrDataType
          'your Error Message here
          Response = acDataErrContinue  
       Case conErrDuplicateKey
          'your Error Message here
          Response = acDataErrContinue   
       Case conErrNullKey
          'your Error Message here
          Response = acDataErrContinue   
       Case Else
          ' It's an unexpected error.  Let Access handle it.
           Response = acDataErrDisplay
    End Select
    End Sub
  • Denburt
    Recognized Expert Top Contributor
    • Mar 2007
    • 1356

    #2
    Sweeeet!!! Thanks I should have implemented somthing along these lines YEARS ago....Will do so immediately, upon returning to work of course.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32669

      #3
      Originally posted by Denburt
      Sweeeet!!! Thanks I should have implemented somthing along these lines YEARS ago....Will do so immediately, upon returning to work of course.
      Echo!
      Nice one ADezii.

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Originally posted by NeoPa
        Echo!
        Nice one ADezii.
        Thanks, NeoPa.

        Comment

        • Geneman
          New Member
          • Mar 2007
          • 17

          #5
          I'll take that error code suggestion one step further.

          Using your code still only gives the user a small obscure message that is not nicely formatted. In many cases all they need is this but, there are some cases when a more detailed explanation is needed.

          In this case, I propose a table of error codes and explanations.

          Thus using your code or a variation thereof.....you could OnERR open a POPUP
          form and have it access a record in the Error Code Table.

          Then in creating that table use your error code numbers as one field and the explanations as the next field.

          The POPUP form would only show the second field, nicely formatted.
          Example: Error 2003

          You have hit the enter key.
          You need to hit the Tab Key.
          When at first you don't suceed.
          Try Try Again.

          Then below this place a button that closes the form labeled
          TRY AGAIN

          This error message not only tells them what they did wrong,
          It tells them how to fix it!

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32669

            #6
            I did something similar once with an error table (I still use). I included the type of continue allowed (You can retry with an ODBC timeout type error for instance). The problem I found was that you never get told when Microsoft change the error list.

            Comment

            • ADezii
              Recognized Expert Expert
              • Apr 2006
              • 8834

              #7
              Originally posted by NeoPa
              I did something similar once with an error table (I still use). I included the type of continue allowed (You can retry with an ODBC timeout type error for instance). The problem I found was that you never get told when Microsoft change the error list.
              Good point, Neo. It's not like they're Intrinsic Constants.

              Comment

              • hyperpau
                Recognized Expert New Member
                • Jun 2007
                • 184

                #8
                Hi,
                I would like to contribute to this. :)
                Information courtesy of FishVal.

                http://bytes.com/forum/thread830828-custom+error+me ssages.html

                Comment

                Working...