custom error handling with macros in access 2010

How can I assign custom error handling using Macros in access 2010 to show specific messages based on the error...with time being a significant factor?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
Actual Macros or VBA code ?

You can't do much with macros, but with VBA, something like this:

Sub YourSubName()
    On Error Goto EH
          ' Your code goes here

    Exit Sub   '<--- Exits normally if there is no error

EH:   '<--- This begins the error handler

    SELECT Case Err.Number

           Case 1234  '<--- Special Handling for specific error numbers
                 Do something
                 Resume Next   '<--- this returns to the line of code following the error and continues from there
            Case 4567 

                   Do Something else

              Case Else

                     MsgBox "Error " & Err.Number & ": " & err.description

            End select

End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DavidLechmanAuthor Commented:
how do I create the number reference list? What part of the texts you provided are examples and/or instructions vs actual code? What would I replace with custom data? Thank you!
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

The numbers 1234 and 4567 are examples (hypothetical error numbers).  You would replace them with actual numbers of errors that you want special handling for.

The lines "Do something" and "Do something else" are examples as well.  You would replace them with lines or blocks of code that handle the errors as you want.

Everything else is "real code".

As far as creating a 'list' of errors... the method I posted here uses a general error message (the Case Else section) to handle *most* errors.

The numbers would be special handling for errors that you might expect to happen in certain while performng specific functions.  

For example, a sub that sends out an Outlook email will encounter a specific error if the user cancels the email.  So that sub should provide error handling for that specific error and if needed use 'resume next' to run the sub to completion after a user-cancelled email.
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
I highly recommend you start with the articles below to get some background on error handling.  Once you complete these, you will pretty much have it under control.

Good white paper on error handling   

Runtime Error Handling 

more on error handling 

Global error handler - awesome product - read my review.      

Discussion of field validating - Form BeforeUpdate event and Form Error event ....

Jeffrey CoachmanMIS LiasonCommented:
MZ Tools has a utility that will automatically insert your custom Error Handling in your code.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.