Home / MS-Access / Getting Started

Automating with Macros

Macros provide a quick and easy way for you to program your Access application to do just what you want. With macros, you can specify customized responses to user actions, such as clicking a button, opening a form, or selecting an option in an options group. Macros can also respond to system conditions, such as an empty recordset. In addition, frequently performed tasks can be automated using macros.

Although the Access control wizards offer an easy way to add user interaction to a form or report, they don't always create exactly the right response. Creating a macro as a response to an event is an alternative to using a control wizard. Writing event procedures in Visual Basic is another alternative, but it requires some skill in programming with precisely constructed commands written in established syntax. Visual Basic for Applications (VBA) procedures are stored with the form or report that activates them, or they're stored as modules that are available from the Modules group in the Navigation Pane. Macros are easier, because you select the actions and their arguments from predefined lists, which virtually precludes any errors in construction. Macros can also be either embedded in the form or report or available as separate Access objects in the Navigation Pane. You might as well start with a macro, because you can always convert the macro to a VBA procedure later, if necessary.

This tutorial discusses how to create macros and how to attach them to events to get the desired response. See how to use macros to find records, display custom dialog boxes, and create an opening home document for an application.

How Macros Work

A macro is a list of one or more actions that work together to carry out a particular task in response to an event. Each action carries out one particular operation. You create the list of actions in the order in which you want them to execute. In addition to selecting the action to be taken, you can specify other details of the action called arguments, which provide additional information, such as which form to open or how to filter the records to be displayed.

You can also set conditions under which the macro action is to be performed, such as to display a message box if a field contains a certain value or is blank. The macro runs only if the condition evaluates to True. If the condition is False, the action is skipped. Then, if another action is in the macro, it's executed. If not, the macro stops.

To run a macro, you assign it to the event property of a form, report, report section, or control. Once the event occurs, the macro automatically executes, beginning with the first action in the list. For example, a macro that opens a form and moves to a blank record for data entry can be assigned to the On Click event property of a command button in a dialog box or another form. When you click the button, the macro executes.