- Uses of Macros
- The Macro Design Facility?An Overview
- Defining Multiple Actions
- Working with Submacros
- Understanding Conditional Expressions
- Using Embedded Macros
- Using Temporary Variables
- Trapping Errors in Macros
- Understanding Macro Actions That Are Not Trusted
- Making Your Application Come Alive with Macros
Defining Multiple Actions
In Access 2010, you can define more than one action within a macro, and you can specify the sequence in which you want the actions performed. The Wedding List Macro database contains several examples of macros that have more than one action. Open the database if it is not open already. Click the Navigation menu at the top of the Navigation pane, click Object Type under Navigate To Category, and then click Macros under Filter By Group to display a list of macros available in the Wedding List Macro database. Right-click the macro named AutoexecXmpl, and then click Design View on the shortcut menu to open the Logic Designer. Figure 20-7 shows the macro.
Figure 20-7 The AutoexecXmpl macro defines multiple actions that Access executes when you run the macro.
If this macro were named AutoExec, Access would execute each action automatically whenever you open the database. This sample macro is an example of a macro that you might design to start the application when the user opens your database.
We defined eight actions in this macro. First, the DisplayHourglassPointer action displays an hourglass mouse pointer to give the user a visual clue that the next several steps might take a second or two. It’s always a good idea to turn on this visual cue even if you think the next several actions won’t take very long. Next, the SelectObject action puts the focus on a known object in the Navigation pane, and the RunMenuCommand-WindowHide action hides the selected window (the Navigation pane).
The next action, OpenForm, opens the WeddingList form. As you can see in Figure 20-7, the OpenForm client action has six arguments that you can use to define how it should work. The Form Name argument indicates the form you want to open. The View argument tells Access what view you want. (The seven choices for the View argument are Form, Design, Print Preview, Datasheet, PivotTable, PivotChart, and Layout.) You can ask Access to apply a filter to the form when it opens either by specifying the name of a query that defines the filter in the Filter Name argument or by entering filter criteria in the Where Condition argument. You can click the Build button on the Where Condition argument to open the Expression Builder, which can help you create the filter.
Edit is the default for the Data Mode argument, which allows the user to add, edit, or delete records while using this form. The other choices for this argument are Add, which opens the form in data entry mode, and Read Only, which opens the form but does not allow any changes to the data. The default setting for the Window Mode argument is Normal, which opens the form in the mode set by its design properties. You can override the design property settings to open the form in Hidden mode, as an icon in the Icon mode, or in the special Dialog mode. When you open a form hidden, the user can reveal it only by adding the Unhide Window command to the Quick Access Toolbar and then clicking the command. When you open a form in Dialog mode, Access does not run further actions or Visual Basic statements until you close that form.
When you select the OpenForm action block on the macro design surface, you’ll see an Update Parameters hyperlink in the lower-right corner of the action block. You can use this link to pass in parameters to the OpenForm action. For example, if the form you are going to open is based on a query that requires parameters, you can use this link to display text boxes beneath the Window Mode argument, where you can set the parameters necessary for the query. We’ll discuss how to pass in parameters with macros in Chapter 21.
Access doesn’t always wait for one action to complete before going to the next one. For example, an OpenForm action merely starts a task to begin opening the form. Particularly if the form displays a lot of data, Access might take several seconds to load all the data and finish displaying the form. Because you’re running Windows, your computer can handle many tasks at once. Access takes advantage of this by going to the next task without waiting for the form to completely open. However, because this macro is designed to maximize the WeddingList form, the form must be completely open for this to work.
You can force a form to finish opening by telling Access to put the focus on the form. This macro does so by using the SelectObject action to identify the object to receive the focus (in this case, the WeddingList form), followed by the GoToControl action to put the focus on a specific control on the form. After the GoToControl action puts the focus on the control, the MaximizeWindow action sizes the active window (the window containing the object that currently has the focus) to fit the entire screen. The final action in the macro (the DisplayHourglassPointer again) restores the mouse pointer to let the user know that the macro is finished.
Learning to define multiple actions within a macro is very useful when you want to automate the tasks you perform on a day-to-day basis. Now that you’ve learned how to do this, the next step is to learn how to group actions by tasks.