Microsoft Access 2010 Bible

(Rick Simeone) #1

Chapter 30: Using Access Macros


1071


The OnError action
The OnError action lets you decide what happens when an error occurs in your macro. This
action has two arguments: Go to and Macro Name. The Go to argument has three settings and
the Macro Name argument is used only with one of these settings, described as follows:

l (^) Next: This setting records the details of the error in the MacroError object but does not
stop the macro. The macro continues with the next action.
l (^) Macro Name: This setting stops the current macro and runs the macro in the Macro
Name argument of the OnError action.
l (^) Fail: This setting stops the current macro and displays an error message. This is the same
as not having error handling in the macro.
The VBA equivalents of these settings are as follows:
On Error Resume Next ‘Next
On Error Goto LABELNAME ‘Macro Name
On Error Resume 0 ‘Fail
The simplest way to add error handling to a macro is to make OnError the first action and set the
Go to argument to Next. This will cause your macro to run without stopping, but you won’t have
any clue which actions ran and which ones didn’t.
Instead, create an error-handling structure. Table 30.6 shows the macro names, actions, and action
arguments for mcrDivisionErrorHandling (shown in Figure 30.17).
TABLE 30.6


mcrDivisionErrorHandling


Macro Name Action Action Argument Action Argument Setting
OnError Go to Macro Name
Macro Name ErrorHandler
SetTempVar Name MyNum
Expression InputBox(“Enter
Numerator.”)
SetTempVar Name MyDenom
Expression InputBox(“Enter
Denominator.”)
MessageBox Message =[TempVars]![MyNum] & “
divided by “ &
[TempVars]![MyDenom] & “ is
“ & [TempVars]![MyNum]/
[TempVars]![MyDenom]
(continued)
Free download pdf