This section provides an introduction to using macros in Access 2003. Not much has changed in the last several versions of Access with respect to macro recording, but if you're just picking up Access 2003 for the first time, this section is for you.
You can use macros for a variety of tasks in Access. Even though it might sound a bit crazy, we usually prefer to write code rather than create a macro. However, that's not always the easiest or most logical method of automation. Access 2003 includes 55 built-in macro commands. Many have additional conditions that can be set. For example, if you choose the OpenDataAccessPage macro action, you'll need to select an existing Data Access Page in your database. You can also choose whether to open the Data Access Page in Browse View or Design View. Other macro actions have similar additional required arguments.
To create a new macro, navigate to the Macros tab of the Access 2003 TaskPane and click New; Access displays the new Macro window, see Figure 2-3. The default name for your new macro is Macroi, but you should change the name when you save the macro. There's nothing inherently wrong with naming your macro Macroi, but it doesn't give you very much of a clue about what the macro is for. It's better to give your macro a descriptive name, such as mcr_OpenForm (which follows the Reddick naming conventions) or even something as simple as GoToRecord (which can be the name of the action the macro performs). Whatever you name your macro (and whether or not you choose to follow Reddick's conventions or create your own conventions) make sure you can easily discern the purpose of the macro when you're looking at your Access 2003 database objects.
When I started programming in Access I didn't have a book like this to learn from. So I created many queries with the name Query1, Query2, Macro1, Macro2, and so on. While the queries and macros I created worked just fine, when I then had to update those databases years later, I couldn't remember what
each individual query and macro did. I had to go through each query and macro one-by-one and rename them according to their purpose before I could update the database. Don't make the same mistakes I did when I started Access development.
Now that you've opened up a blank macro, click the first line of the Action column to display the Actions drop-down menu shown in Figure 2-4.
To implement an action, click the Action name to add it to your macro. Depending on the action you choose, you'll see additional criteria appear in the Action Arguments section of the window. Not all actions have arguments. In particular the Beep, CancelEvent, FindNext, Maximize, Minimize, Restore, ShowAllRecords, and StopMacro actions don't have arguments. Figure 2-5 shows a macro with several different actions. The Action Arguments is shown for the OpenForm action.
For readability, some Access programmers like to group their actions in a macro, leaving a blank line between groups of actions. There's nothing wrong with this practice; however, there's no advantage to it either.
Now that you've completed your macro, save you changes and exit. However, what good is a macro if you don't have a way to call it? One of the common uses for a macro and one of the easiest ways to use one is in response to the click event of a command button on a form. To associate a macro with the click event of a command button, use the following steps:
1. Within the design of your form, choose a command button.
2. Click the Properties toolbar button to display the properties window for the command button.
3. Click the Event tab of the Properties dialog box.
4. Click in the OnClick line of the Properties dialog box to display the drop-down arrow.
5. From the drop-down list choose the name of your macro. All macros in your database are listed in the drop-down list.
6. Save and run your form. Clicking the command button will run each action in the macro sequentially.
You can also call macros from within your code. You might wonder why you would ever call a macro from within code. After all, you're already writing code, why not just write code to accomplish the steps in the macro? Well, we can't give you a definitive answer to that question, except to say that if you already have a perfectly good macro that does what you need, why not use it? Writing code to duplicate a working macro is like taking two steps backward for every one step forward. On the other hand, sometimes you just want everything in one place. If so, go ahead and duplicate your macro actions within code.
Was this article helpful?