3 4
Macros are used to run database actions automatically, as an alternative to performing the actions manually or writing VBA code. Macros are limited in functionality comparedto VBA code, but they’re also easier to learn to write. Even if you’re an expert code writer, you’ll find that several special types of macro are still genuinely useful.
Most database users will find an AutoKeys macro useful, to provide a set of custom hot keys. Some databases can use an AutoExec macro, because even though you can specify a database’s startup form and many other database behaviors in the Startup dialog box, you might need to perform some special tasks when a database is opened. For example, you might want to create a Word or an Excel object for the database to work with, create a lookup table filled with dates calculated from the current data, or set the value of a global variable.
A third special macro type (which doesn’t have a name) is a macro that runs a series of functions and/or action queries to automate the import and cleanup of data from an external data source. These three types of special macros are described in the section "Creating Special-Purpose Macros."
To create a simple macro, follow these steps:
Figure 14-1. Use the Action column to select an action to add to a new macro.
tip
note
By default, each macro has only two columns: Action and Comment. These two columns are all you need to create a simple macro. However, two other columns—Macro Names and Conditions—can be made visible by clicking the appropriate buttons on the Macro Design toolbar, shown in Figure 14-2.
Figure 14-2. The Macro Names and Conditions buttons appear on the Macro Design toolbar.
The Macro Names column is used to enter macro names, which are used when you create a macro group (as opposed to a single macro). (See the sidebar "Macros and Macro Groups," for more details on macro groups.) The Conditions column is used to enter conditions that must be met before a macro action is run. For example, you could enter a condition specifying that a certain form must be open or that a field must have a certain value. Figure 14-3 shows a portion of the Suppliers macro group from the Northwind sample database, with macro names and a condition for a MsgBox action that displays the message box only if the SupplierID field is Null.
Figure 14-3. A macro group uses macro names to identify individual macros.
A macro condition is the equivalent of a simple If…Then construct in code. To use more complex logical structures, such as Select Case statements or nested If…Then structures, you need to write VBA code. If you need to deal with just one of two alternatives (say, a form that is open or closed), however, a macro condition can handle this situation very well.
See Chapter 20, "Customizing Your Database Using VBA Code," for more details on writing VBA code. See the section "Creating Macro Conditions," for more details on creating macro conditions.
If you want the Macro Names and Conditions columns to be visible by default in all new macros, select the appropriate check box on the View tab in the Options dialog box shown in Figure 14-4 (choose Tools, Options to open this dialog box). This step will save you time if you often create macro groups or macros with conditions.
Figure 14-4. You can specify that the Conditions column should be visible by default on the View tab of the Options dialog box.
Macros and Macro Groups
A macro group is not a separate type of database object; it is simply a set of macros, each identified by name in the Macro Names column. Macro groups are created in theMacro Designer, just as single macros are. The only difference is that in a macro group, a group of macro actions is identified by name as belonging together. In earlier versions of Access, a macro group with a set of macros designed to run from various events of a form was often used to automate form events. This use of macro groups has for the most part been superseded by VBA event procedures stored in a form’s class module.
The AutoKeys macro, described in the section "Using the AutoKeys Macro Group," is an example of a macro group.
Some of the most frequently used macro actions are listed here, with a description of their usage:
tip