15.4. Managing Macros
As you build more and more snazzy macros, you'll need some way to keep them all organized, and make sure the macros you need are at your fingertips when you need them. Access gives you a few tools to help, including macro groups, which combine related macros into one object for easier storage, and macro shortcut keys, which let you trigger the right macro exactly when you need it.
15.4.1. Macro Groups
The average macro's only three to five actions long. However, the average database that uses macros quickly accumulates dozens of them. Managing these tiny programs can become quite a headache , especially when you need to remember what each macro does.
You could use a macro group. On the surface, a macro group looks like one macro, because it's stored in a single database object. However, a macro group can contain a nearly unlimited number of individual macros, each with its own name . Once you put related macros together in a single group, you'll have an easier time finding the macro you need when it's time to edit it.
Tip: Access masters use macro groups to group together macros that you use on the same form, macros that work with the same table, or macros that perform a similar type of task (like printing or record editing).
To create a macro group, you simply create a macro that uses the Macro Name column. Ordinarily, the Macro Name column's hidden because all actions are part of the same macro. But when creating a macro group, you must select Macro Tools Design Show/Hide Macro Names to pop it into view.
Now here's the trick. Each time you start a new macro, you fill in the name for that macro in the Macro Name column. This way, it's possible to stack one macro on top of another, so long as you remember to keep changing the name. You can even use blank rows to help keep them separate and make the macro group easier to read and understand. The best way to understand it is to check out the sample group in Figure 15-15.
Tip: It's easiest to use macro groups with short macros (macros that don't have too many actions). When you need to edit your macros, you can use the ribbon's handy Insert Rows command to make more space.
Every macro in a macro group has a two-part name. The first part's the macro group, and the second part's whatever text you put in the Macro Name column. The PrintCheapskates macro in the BasicMacros group that's shown in Figure 15-15 has the full name BasicMacros.PrintCheapSkates. You need to use the full name when you want to run the macro.
One limitation to macro groups is that you can't use them from the navigation pane. If you right-click a macro group in the navigation pane, and then choose Run, Access runs only the first macro in the group. To run a different macro, you need to choose Database Tools Macro Run Macro. You can then type in the right two-part name or pick it from a list (as shown in Figure 15-16).
Note: If this sounds like too much work, don't worry. Most macros aren't launched through the navigation pane, but linked to a form, in which case the two-part name doesn't require any extra work. But if you have a macro that you do want to run from the navigation pane, grouped macros obviously aren't the way to go.
15.4.2. Assigning a Macro to a Keystroke
Occasionally, you'll create a macro that's just so handy you want it at your fingertips at all times. You can make this happen by assigning your macro to a key combination. Then, instead of heading to the navigation pane, you hit something like Ctrl+M, and your macro springs into action right away.
Tip: Keys are valuable . Only assign a macro to a key combination if you know that you'll use that macro often, and with many different forms or tables.
Oddly enough, the way you assign key combinations in Access is by creating another macro. This macro must be named AutoKeys, and its sole purpose in life is to assign keystrokes to other macros.
So how does AutoKeys work? It's all in the name. When you add a macro to the AutoKeys group, you give it a specially coded name that's really a keystroke combination. If you name the macro ^ M , Access knows to trigger it when you press Ctrl+M on the keyboard. Figure 15-17 shows a few macro examples.
Note: In Figure 15-17, notice that each macro in the group runs a separately stored macro object using the RunMacro action. This design isn't necessary (you could code each macro right inside the AutoKeys group by including all the necessary actions), but it improves organization. It also gives you better flexibility, because you can choose to use a different set of macros with the same key combinations without removing the macro from the AutoKeys group.
The only trick to using the AutoKeys macro group is knowing how to name your macro so Access uses the key combination you want. Access lets you use letters and numbers , in combination with the Ctrl and Shift keys. (The Alt key is off-limits, because it lets you choose commands on the ribbon, as described in Section 3.2.1.) Additionally, you can use the function keys (F1 through F12), and the Insert and Delete keys, also in conjunction with Ctrl and Shift.
Here's how you name your macros:
Tip: Before you assign a macro to a key combination, you should check that the keystroke doesn't already do anything useful. Your macro overrides any built-in Access commands. One example's Ctrl+S, which saves the current object. To help reduce the chance of keystroke collisions, you can use keystrokes that involve the Shift key, which are less commonly used.
15.4.3. Configuring a Startup Macro
Every once in a while, you'll create a macro that's so important you want to call it into action as soon as you open a database. Perhaps this macro opens a few important forms and reports , imports data from another file, or runs a clean-up query. No matter what the reason, Access makes it easy to launch a startup macro. All you need to do is name your macro AutoExec.
Access also gives you a way to sidestep the AutoExec macro. If you hold down the Shift key while a database is first loading, then Access doesn't run the AutoExec macro (and it doesn't show any startup form you may have configured, as described in Section 14.2.2). However, don't rely on this dodge, because it's all too easy to forget to hold Shift down at the right time.
Tip: Remember, if your macro contains unsafe actions and your database isn't trusted, Access doesn't run it. If you open an untrusted database, and choose to turn on the database using the message bar (as described in Section 15.2.2), Access reloads the database, at which point it runs the AutoExec macro.