Run macros

You can run a macro (macro: An action or set of actions that you can use to automate tasks.) directly, in a macro group (macro group: A collection of related macros that are stored together under a single macro name. The collection is often referred to simply as a macro.), from another macro or an event procedure (event procedure: A procedure that is automatically executed in response to an event initiated by the user or program code, or that is triggered by the system.), or in response to an event (event: An action recognized by an object, such as a mouse click or key press, for which you can define a response. An event can be caused by a user action or a Visual Basic statement, or it can be triggered by the system.) that occurs on a form, a report, or a control (control: A graphical user interface object, such as a text box, check box, scroll bar, or command button, that lets users control the program. You use controls to display data or choices, perform an action, or make the user interface easier to read.) on a form or report.

ShowRun a macro

ShowRun a macro that is in a macro group

To run a macro (macro: An action or a set of actions that you can use to automate tasks. Macros are recorded in the Visual Basic for Applications programming language.) that is in a macro group (macro group: A collection of related macros that are stored together under a single macro name. The collection is often referred to simply as a macro.), do one of the following:

macrogroupname.macroname

For example, this event property setting runs a macro called Categories in a macro group called Form Switchboard Buttons:

Forms Switchboard Buttons.Categories

ShowRun a macro from another macro or from a Microsoft Visual Basic procedure

Add the RunMacro action to your macro or procedure.

DoCmd.RunMacro "My Macro"
                  

ShowRun a macro or event procedure in response to an event on a form, report, or control

Microsoft Access responds to many types of events on forms, reports, and controls, including mouse clicks, changes in data, and forms or reports being opened or closed.

  1. Open the form or report in Design view (Design view: A view that shows the design of these database objects: tables, queries, forms, reports, and macros. In Design view, you can create new database objects and modify the design of existing objects.).
  2. Display the property sheet for the form or report, or for a section or control on the form or report.
  3. Click the Event tab.
  4. Click the event property for the event that you want to trigger the procedure. For example, to display the event procedure for the Change event, click the OnChange property.
  5. Click Build Button image next to the property box to display the Choose Builder dialog box.
  6. Do one of following:

Create the macro

ShowHow?

  1. In the Choose Builder dialog box, choose Macro Builder and click OK.
  2. In the Save As dialog box, enter a name for the macro and click OK.
  3. Add an action (action: The basic building block of a macro; a self-contained instruction that can be combined with other actions to automate tasks. This is sometimes called a command in other macro languages.) to the macro.

ShowHow?

  1. In the Macro window (Macro window: The window in which you create and modify macros.), click the first empty row in the Action column. If you want to insert an action (action: The basic building block of a macro; a self-contained instruction that can be combined with other actions to automate tasks. This is sometimes called a command in other macro languages.) between two action rows (action row: A row in the upper part of the Macro object tab in which you enter macro names, actions, arguments, and comments associated with a particular macro or macro group.), click the selector for the action row just below the row where you want to insert the new action, and then click Insert Row Button image on the toolbar.
  2. In the Action column, click the arrow to display the action list (action list: The list that appears when you click the arrow in the Action column of the Macro object tab.).
  3. Click the action you want to use.
  4. In the lower part of the window, specify arguments for the action, if any are required. For action arguments (action argument: Additional information required by some macro actions. For example, the object affected by the action or special conditions under which the action is carried out.) whose settings are a database object (database objects: An Access database contains objects such as tables, queries, forms, reports, pages, macros, and modules. An Access project contains objects such as forms, reports, pages, macros, and modules.) name, you can set the argument by dragging the object from the Database window (Database window: In Access 2003 and earlier, the window that appears when a database or project is opened. It displays shortcuts for creating new database objects and opening existing objects. In later versions, it is replaced by the Navigation Pane.) to the action's Object Name argument box.
  5. Type a comment for the action. Comments are optional.
  1. To add more actions to the macro, move to another action row (action row: A row in the upper part of the Macro object tab in which you enter macro names, actions, arguments, and comments associated with a particular macro or macro group.) and repeat step 3. Microsoft Access carries out the actions in the order you list them.

Create an event procedure

ShowHow?

You can set an event property (event property: A named attribute of a control, form, report, data access page, or section you use to respond to an associated event. You can run a procedure or macro when an event occurs by setting the related event property.) for a form, report, or control (control: A graphical user interface object, such as a text box, check box, scroll bar, or command button, that lets users control the program. You use controls to display data or choices, perform an action, or make the user interface easier to read.) to [Event Procedure] to run code in response to an event. Microsoft Access creates the event procedure (event procedure: A procedure that is automatically executed in response to an event initiated by the user or program code, or that is triggered by the system.) template for you. You can then add the code you want to run in response to the particular event.

  1. Double-click Code Builder to display the event procedure Sub and End Sub statements in the form module (form module: A module that includes Visual Basic for Applications (VBA) code for all event procedures triggered by events occurring on a specific form or its controls.) or report module (report module: A module that includes Visual Basic for Applications (VBA) code for all event procedures triggered by events occurring on a specific report or its controls.). These statements define, or declare, the event procedure.

Microsoft Access automatically declares event procedures for each object and event in a form or report module by using the Private keyword to indicate that the procedure can be accessed only by other procedures in that module.

  1. Add the code to the event procedure that you want to run when the event occurs. For example, to produce a sound through the computer's speaker when data in the CompanyName text box changes, add a Beep statement to the CompanyName_Change event procedure, as follows:
Private Sub CompanyName_Change()
    Beep
End Sub
                              

The event procedure runs each time the Change event occurs for the object.

  1. Saving the macro or procedure will set the appropriate event property (event property: A named attribute of a control, form, report, data access page, or section you use to respond to an associated event. You can run a procedure or macro when an event occurs by setting the related event property.) to the name of the macro, or to [Event Procedure] if you're using an event procedure.

For example, to use a macro to display a message when you click a command button, set the command button's OnClick property to the name of a macro that displays the message. To use an event procedure, create a Click event procedure for the command button, and then set its OnClick property to [Event Procedure].

 
 
Applies to:
Access 2003