9.4. Cleaning Up After an Error

Errors often occur in the middle of a lengthy procedure, when all kinds of things are happening. Many settings or values persist after an error occurs, and it's up to you to make sure they are reset back to their appropriate values. For example, these situations may be true when an unexpected error occurs in your procedure:

  • Objects are open.

  • The hourglass is on.

  • You have set the status bar text or a progress meter.

  • Warnings are off.

  • You are in a transaction that should be rolled back if an error occurs.

Although your code may clean up all these settings under normal circumstances, a common mistake is to leave a mess when your code encounters an error. You don't want to leave a mess, do you?

Neglecting to clean up can cause problems, ranging in severity from annoying to serious. For example, if you don't turn the hourglass off, it will remain on while your users continue their work in Access. That's just annoying.

More seriously, if you don't turn DoCmd.SetWarnings back to True, any action queries (such as an Update or Delete query) will modify or delete data without any warning. Obviously, that can cause some serious problems that neither you nor your users will appreciate.

Have you ever seen an Access application that won't close? Even when you click the X button, or run a DoCmd.Quit in your code, Access just minimizes instead of closing. This can be quite mysterious. Many reasons have been identified for this behavior, but one of them is related ...

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.