You’ll find that most of the forms you design for an application require multiple macros to respond to events-some to edit fields, some to open reports, and still others to respond to command buttons. You could design a separate macro saved with its own unique name in the Database window to respond to each event, but you’ll soon have hundreds of macros in your application.
You can create a simpler set of more manageable objects by defining macro objects that contain several named macros within each object. (This sort of macro object is called a macro group within Access 2007 Help.) One approach is to create one saved macro object per form or report. Another technique is to categorize macros by type of action-for example, one macro containing all the OpenForm actions and another containing all the OpenReport actions.
Let’s take a look at a form that depends on a macro group. Figure 18–8 shows the PrintOptions form from the Wedding List Macro database in Form view. This form contains two command buttons, Print and Cancel, each of which triggers a different macro. The two macros are contained within a macro object called DoReport.
Figure 18–8: The two command buttons on the PrintOptions form run macros.
To look at the macro object, right-click the DoReport macro in the list of macro objects in the Navigation Pane, and then click Design view on the shortcut menu to open this macro object in the Macro window. Figure 18–9 shows the macro.
Figure 18–9: The DoReport macro group includes nine individual macros.
To create a group of named macro procedures within a macro object, you must open the Macro Name column in the macro design window. (If you don’t see the Macro Name column, click the Macro Names button in the Show/Hide group on the Design tab.) You can create a series of actions at the beginning of the macro definition, without a name, that you can reference from an event property or a RunMacro action by using only the name of the macro object. As you saw earlier in the AutoexecXmpl macro, naming a macro object in a RunMacro action (without any qualifier) asks Access to run the unnamed actions it finds in that macro object.
To create a set of named actions within a macro object, place a name on the first action within the set in the Macro Name column. To execute a named set of actions within a macro object from an event property or a RunMacro action, enter the name of the macro object, a period, and then the name from the Macro Name column. For example, to execute the PrintIt set of actions in the DoReport macro, enter DoReport.PrintIt in the event property or the Macro Name parameter.
In the sample DoReport macro, each of the nine names in this column represents an individual macro within the object. (You must scroll down to see the other names.) The first macro, Options (triggered by the Print Report button on the WeddingList form), opens the PrintOptions form, and the second macro, PrintIt, determines which report was selected. The next four macros, Groups, Alpha, Accepted, and PrintF, display the appropriate report in Print Preview mode, based on the result of the second macro. The Cancel macro merely closes the PrintOptions form if the user clicks the Cancel button. The NoRecords macro cancels opening a report when the report’s record source has no data, and the ErrReport macro handles errors. As you might have guessed, Access runs a macro starting with the first action of the macro name specified and executes each action in sequence until it encounters a StopMacro action, another macro name, or no further actions. As you’ll see later, you can control whether some actions execute by adding tests in the Condition column of the macro.
If you open the PrintOptions form in Design view (see Figure 18–10) and look at the properties for each of the command buttons, you’ll see that the On Click property contains the name of the macro that executes when the user clicks the command button. If you open the list for any event property, you can see that Access lists all macro objects and the named macros within them to make it easy to select the one you want.
Figure 18–10: You can see that Access lists all macro objects and named macros in the various event properties.
Remember, the macro name is divided into two parts. The part before the period is the name of the macro object, and the part after the period is the name of a specific macro within the object. So, for the first command button control, the On Click property is set to DoReport.PrintIt. When the user clicks this button, Access runs the PrintIt macro in the DoReport macro object. After you specify a macro name in an event property, you can click the Build button next to the property, and Access opens that macro in a Macro window.