9.2. Two Kinds of Errors: Unexpected and Expected

All errors that your Access application may encounter fall into one of two categories: unexpected and expected. The following sections explain these two categories and what your application should do when errors occur in each of them.

9.2.1. Handling Unexpected Errors

Unexpected errors are ones that you have no way of predicting, and that under normal circumstances should not occur. When your application encounters an unexpected error (for example, divide by zero or a missing object), and no error handling is in effect, Access displays an error message like the one shown earlier and abruptly ends the procedure.

The goal of error handling in this case is not to solve the problem the error is indicating—there's nothing you can do about that now. Your code has tripped on an error and fallen down. The only thing you can do is let the user know what happened calmly and in plain language. Figure 9-2 is an example of what your error message might look like.

Figure 9.2. Figure 9-2

There are several differences between the error message Access shows and the "handled" error message you can show:

  • You can specify the title of the message box instead of displaying "Microsoft Visual Basic" or "Microsoft Access."

  • You can show an icon to have a stronger impact.

  • You can add a text explanation. You can even mention your phone number or other contact ...

Get Access™ 2007 VBA Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.