Testing a Macro

Although Access doesn't offer very sophisticated tools for testing and debugging 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 on the toolbar or select Run | Single Step.

  3. To execute the macro, click Run. Access executes the first line of the macro, and the Macro Single Step dialog box appears, showing the macro name, condition, action name, and arguments (see Figure 13.18). In the figure, the macro name is mcrPersonalInfo, the condition evaluates to True, and the action name is MsgBox. The MsgBox arguments are You Are Over a Quarter Century Old, Yes, and Information.

    Figure 13.18. Viewing the macro name, condition, action name, and arguments for the current step of a macro.

    graphics/13fig18.jpg

  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 Halt button. To continue normal execution of the macro without stepping, click the Continue button.

Task: Stepping Through a Macro

graphics/todo_icon.gif

It's easiest to learn about stepping through a macro by experiencing it firsthand. Here's what you do:

  1. Open (in Design view) the mcrPersonalInfo macro that you created earlier in this hour.

  2. Click the Single Step button on the Macro Design toolbar. Run the frmPersonalInfo form, also created earlier in this hour.

  3. Select a gender and type in a birth date.

  4. Click the Check Gender command button; this should invoke the Macro Single Step dialog box.

  5. Step through the macro one step at a time. View the macro name, condition, action name, and arguments for each step.

  6. Change the gender and run the macro again. Carefully observe how this affects the macro's execution.

  7. Click the Check Birth Date command button. Step through the macro one step at a time, viewing whether the condition evaluates to True or False.

  8. After the macro ends, try entering a different value for the birth date. Step through the macro again and carefully observe whether the condition evaluates to True or False for each step.

As you can see, although Microsoft supplies some tools to help you debug a macro, you will probably agree that they're quite limited. That's one reason most experienced developers prefer to develop applications by using VBA code.

graphics/book_icon.gif

The Single Step button on the Macro Design toolbar toggles. When you activate Step mode, you activate it for all macros in the current database and all other databases until you either turn the toggle off or exit Access. This 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 you run another macro and that macro unexpectedly goes into Step mode.




    Part III: Creating Your Own Database and Objects
    Part V: Advanced Topics