Other New Features Available in Macros
There were two major
improvements introduced with Access 2007 macros. The first is the
introduction of error handling, and the second is the introduction of
variables. Notice the OnError macro action in Figure 1. The example branches to a macro named ErrorHandler in the case of an error. Unlike previous versions of Access, where error handling in macros was virtually nonexistent, the new OnError macro action provides similar error handling to that of VBA code (the programming language for Microsoft Access).
Another exciting addition to Access 2007 macros was the introduction of variables. The new SetTempVar macro action enables you to create a variable and assign it a value. Figure 2 provides an example. Notice in the figure that the macro uses the SetTempVar action to create a variable called CurrentDate and assign it the value returned from the built-in Date() function.
Testing a Macro
Although Access doesn’t
offer very sophisticated tools for testing and debugging your macros,
it does give you a method for stepping through each line of a macro:
1. | Open the macro in Design view.
|
2. | Click Single Step in the Tools group of the Design tab.
|
3. | To
execute the macro, click Run. The first line of the macro is executed,
and the Macro Single Step dialog box appears, showing you the Macro
Name, Condition, Action Name, and Arguments, as in Figure 3. In the figure, the macro name is mcrPersonalInfo, and the condition evaluates to false. The action name and arguments are not available.
|
4. | To
continue stepping through the macro, click the Step button on the Macro
Single Step dialog box. If you want to halt the execution of the macro
without proceeding, click the Stop All Macros button. To continue normal
execution of the macro without stepping, click the Continue button.
|
The Single Step button in the
Tools group of the Design tab is a toggle. After you activate Step
Mode, it’s activated for all macros in the current database and all
other databases until you either turn off the toggle or exit Access.
This behavior can be quite surprising if you don’t expect it. You might
have invoked Step mode in another database quite a bit earlier in the
day, only to remember that you forgot to click the toggle button when
some other macro unexpectedly goes into Step mode.