Defining Multiple Actions

In Access 2007, 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 Macro window. Figure 18–7 shows the macro.

image from book
Figure 18–7: The AutoexecXmpl macro defines multiple actions that Access executes when you run the macro.

Inside Out-Autoexec Macros-A Special Type of Macro 

If you create a macro and name it Autoexec, Access runs the macro each time you open the database in which it is stored. The preferred method to run startup code is to define a startup form in the Application Options section of the Current Database category in the Access Options dialog box. For details, see Chapter 24, “The Finishing Touches.”

In the Wedding List Macro sample database, the Autoexec macro examines the IsTrusted property of the CurrentProject object (CurrentProject defines all the executable code) to see whether the database is trusted. (You can trust the database by placing it in a trusted location.) If the database is not trusted, the macro opens a dialog form with instructions on how to create a trusted folder. If you open this database in a trusted location, the macro displays a copyright form followed by a message box telling you to run the AutoexecXmpl macro to start the application.

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 you might design to start the application when the user opens your database.

We defined eight actions in this macro. First, the Hourglass 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 RunCommand-WindowHide action hides the selected window (the Navigation Pane).

The next action, OpenForm, opens the WeddingList form. As you can see in Figure 18–7, the OpenForm 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 in the argument box and then click the Build button that Access reveals 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 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.

Access doesn’t always wait for one action to complete before going on 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 in order 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 Maximize 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 Hourglass again) restores the mouse pointer to let the user know that the macro is finished.


Because macros might be used by inexperienced programmers, Access automatically restores Hourglass when it finishes running a macro. If it didn’t do this, the mouse pointer would continue to show an hourglass. The user would think that Access is broken. However, it’s good practice to always restore what you turn off, which is why the sample AutoexecXmpl macro includes Hourglass-No at the end even though it isn’t required. As you’ll learn in the next chapter, Visual Basic isn’t quite so forgiving. If you turn the mouse pointer to an hourglass in a Visual Basic procedure and forget to turn it back on before your code exits, your mouse pointer will forever display an hourglass!

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.

Microsoft Office Access 2007 Inside Out
MicrosoftВ® Office Access(TM) 2007 Inside Out (Microsoft Office Access Inside Out)
ISBN: 0735623252
EAN: 2147483647
Year: 2007
Pages: 234

Similar book on Amazon
Access 2007: The Missing Manual
Access 2007: The Missing Manual
Microsoftu00ae Office Access(TM) 2007 Step by Step (Step By Step (Microsoft))
Microsoftu00ae Office Access(TM) 2007 Step by Step (Step By Step (Microsoft))
Microsoft Office Access 2007 Forms, Reports, and Queries
Microsoft Office Access 2007 Forms, Reports, and Queries
Alison Balter's Mastering Microsoft Office Access 2007 Development
Alison Balter's Mastering Microsoft Office Access 2007 Development © 2008-2017.
If you may any questions please contact us: