Testing and Debugging a Macro - MS-Access Tutorial
Testing and Debugging a Macro
After you complete the macro, you can run it to see if it behaves as planned. You have a choice of running the complete macro at once or stepping through the macro one action at a time. If an error occurs in the macro or you don't get the results you expect, use the Single Step method of running the macro to see what went wrong.
Starting the Macro
After you finish adding the actions and setting the arguments, you can run a macro in several ways. While still in the Macro window, on the Design tab's Tools group, click the Run command. After you name and save a standalone macro, you can run it from the Navigation Pane by one of the following methods:
- Double-click the macro name.
- Right-click the macro name and choose Run from the shortcut menu.
If your macro is embedded in a particular form or report, open the form or report in Form or Report view and perform the task that activates the macro. You can also open the Macro window from the form or report event property and run it by clicking the Run command.
If an error occurs during the operation, Access displays an error message explaining the reason for the error. In this case, we left out the required Object Name argument. Read the message, and then click OK to open the Action Failed dialog box. This tells you which action in the macro failed and the arguments being used at the time. The following Arguments box shows that the second argument is missing from the GoToRecord action. You can tell by the two commas with no argument between them. It also shows any conditions that were in effect. Your only option in this dialog box is to click Stop All Macros to stop the macro. Before closing the dialog box, note the action name, error number, and other data about where the fault occurred. Then, it's up to you to switch to the Macro window to correct the problem.
NOTE The error number shown in the Action Failed dialog box is new to Access 2007. You can use it to create a macro or procedure that can respond to that particular type of error.
Stepping through a Macro
If you create a macro with many actions and it contains an error, you can use the Single Step method to move through the macro, one action at a time. You must be in the Macro window to step through the macro actions. To start stepping through the macro, click the Single Step command in the Tools group, and then click the Run command to carry out the first action. A Macro Single Step dialog box opens showing the details of the first step in your macro.
NOTE: The Single Step mode remains in effect until you turn it off by clicking the Single Step command again, or choosing Run and clearing the Single Step command.
Your options in this dialog box are as follows:
- Step (default) Moves to the next action.
- Stop All Macros Stops macro execution.
- Continue Stops Single Step mode and runs the rest of the macro without stopping. If another error occurs, the macro stops and an Action Failed dialog box appears.
NOTE: Your macro might cause other macros to run. For example, an OpenForm action in a macro might open a form that has other macros assigned to the OnOpen, OnLoad, or other event properties. If other macros run as a result of the macro that you're testing, their steps are also displayed in the Macro Single Step dialog box. You can tell it's a different macro by the name that appears in the Macro Name box.
Modifying a Macro
After you see how a macro runs, you might decide to make some changes to it, such as adding another action, changing the order of the actions, adding a condition to the action, adding a Where Condition argument to limit the records, or creating additional macros to include in a macro group.
To open a standalone macro for modification, right-click the macro name in the Navigation Pane and choose Design View. If the macro is embedded, open the form or report in Design or Layout view and open the Macro window by clicking the Build button in the Property Sheet and choosing Macro Builder.
Use the Insert Rows and Delete Rows commands in the Rows group, or right-click in the macro design and choose a command from the shortcut menu to add or delete actions. You can also use the standard cut, copy, and paste operations to edit a macro. The Undo button is available on the Quick Access toolbar to reverse the recent changes, as well.
TIP: If the macro operates on important data, make a temporary copy of the data to work with during the modification process. This way, if anything goes wrong, you haven't destroyed valuable information.
After making the changes to the macro, save it again. If you save it with a different name, be sure to change all of the references to the macro accordingly.