Run macros

Microsoft Office Access 2003

To run a macro directly, do one of the following:

  • To run a macro from the Macro window, click Run Button image on the toolbar.
  • To run a macro from the Database window, click Macros Button image and then double-click a macro name.
  • On the Tools menu, point to Macro, click Run Macro, and then select the macro from the Macro Name list.

ShowRun a macro that is in a macro group

To run a macro that is in a macro group, do one of the following:

  • Specify the macro as an event property setting on a form or report or as the Macro Name argument of the RunMacro action. Refer to the macro using this syntax:

    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

  • On the Tools menu, point to Macro, click Run Macro, and then select the macro from the Macro Name list. When macro names appear in lists, Microsoft Access includes an entry for each macro in each macro group in the format macrogroupname.macroname.
  • Run a macro that is in a macro group from a Microsoft Visual Basic procedure by using the RunMacro method of the DoCmd object, with the syntax shown previously to refer to the macro.

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

Add the RunMacro action to your macro or procedure.

  • To add the RunMacro action to a macro, click RunMacro in the action list in a blank action row, and set the Macro Name argument to the name of the macro you want to run.
  • To add the RunMacro action to a Visual Basic procedure, add the RunMacro method of the DoCmd object to your procedure, and specify the name of the macro you want to run; for example, the following RunMacro method runs the macro My Macro:
    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.
  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 to the macro.

      ShowHow?

      1. In the Macro window, click the first empty row in the Action column. If you want to insert an action between two action rows, 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.
      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 whose settings are a database object name, you can set the argument by dragging the object from the Database window to the action's Object Name argument box.
      5. Type a comment for the action. Comments are optional.
    4. To add more actions to the macro, move to another action row 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 for a form, report, or control to [Event Procedure] to run code in response to an event. Microsoft Access creates the event procedure 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 or report module. 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.

    2. 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.

  7. Saving the macro or procedure will set the appropriate 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].