Microsoft Access 2010 Bible

(Rick Simeone) #1

Chapter 30: Using Access Macros


1073


The first OnError action in the macro lets Access know to move to the submacro
ErrorHandler when an error occurs. If an error occurs (by entering 0 as the denominator), the
macro stops and moves to the ErrorHandler submacro. The ErrorHandler submacro dis-
plays a message box — using the MacroError object (described in the next section) to display
the error’s description in the Message and the error’s number in the Title, using the following
expressions:

[MacroError].[Description]
[MacroError].[Number]

After the error handler’s message box, the ClearMacroError action clears the MacroError
object. The RunMacro action moves execution to the macro’s Cleanup submacro. The Cleanup
section of the macro removes the temporary variables.

Note
There’s no Resume functionality in macro error handling. If you want to run additional code after the error-
handling actions, you must use the RunMacro action in the error-handling submacro to run another macro, or
place the actions in the error handler.


The RunMacro action also appears after the MessageBox action in the main section of the
macro. Because you’re using submacros, the macro stops after it reaches the ErrorHandler sub-
macro. In order to force the cleanup of the temporary variables, use the RunMacro action to run
the Cleanup submacro. Otherwise, you’d have to put the RemoveTempVar actions in the main
section and in the ErrorHandler section of the macro.

The MacroError object
The MacroError object contains information about the last macro error that occurred. It retains
this information until a new error occurs or you clear it with the ClearMacroError action. This
object contains a number of read-only properties you can access from the macro itself or from
VBA. These properties are as follows:

l (^) ActionName: This is the name of the macro action that was running when the error
occurred.
l (^) Arguments: The arguments for the macro action that was running when the error
occurred.
l (^) Condition: This property contains the condition for the macro action that was running
when the error occurred.
l (^) Description: The text representing the current error message — for example, Divide
by Zero or Type Mismatch.
l (^) MacroName: Contains the name of the macro that was running when the error occurred.
l Number: This property contains the current error number — for example, 11 or 13.

Free download pdf