RunMacro Action

Description

You can use the RunMacro action to run a macro. The macro can be in a macro group.

You can use this action:

  • To run a macro from within another macro.
  • To run a macro based on a certain condition.
  • To attach a macro to a custom menu command.
Setting

The RunMacro action has the following arguments.

Action argument

Description

Macro Name

The name of the macro to run. The Macro Name box in the Action Arguments section of the Macro window shows all macros (and macro groups) in the current database. If the macro is in a macro group, it's listed under the macro group name in the list as macrogroupname.macroname. This is a required argument.

If you run a macro containing the RunMacro action in a library database, Microsoft Access looks for the macro with this name in the library database and doesn't look for it in the current database.

Repeat Count

The maximum number of times the macro will run. If you leave this argument blank (and the Repeat Expression argument is also blank), the macro runs once.

Repeat Expression

An expression that evaluates to True (–1) or False (0). The macro stops running if the expression evaluates to False. The expression is evaluated each time the macro runs.


Remarks

If you enter a macro group name for the Macro Name argument, Microsoft Access runs the first macro in the macro group.

This action is similar to clicking Macro on the Tools menu, which you can use to select and run a specified macro. However, the command runs the macro only once, whereas the RunMacro action can run a macro as many times as you want.

Tip You can use the Repeat Count and Repeat Expression arguments to determine how many times the macro runs:

  • If you leave both arguments blank, the macro runs once.
  • If you enter a number for Repeat Count but leave Repeat Expression blank, the macro runs the specified number of times.
  • If you leave Repeat Count blank but enter an expression for Repeat Expression, the macro runs until the expression evaluates to False.
  • If you enter values for both arguments, the macro runs the number of times specified in Repeat Count or until Repeat Expression evaluates to False, whichever occurs first.
When you run a macro containing the RunMacro action, and it reaches the RunMacro action, Microsoft Access runs the called macro. When the called macro has finished, Microsoft Access returns to the original macro and runs the next action.

Notes

  • You can call a macro in the same macro group or in another macro group.
  • You can nest macros. That is, you can run macro A, which in turn calls macro B, and so on. In each case, when the called macro has finished, Microsoft Access returns to the macro that called it and runs the next action in that macro.
Tip You can use the RunMacro action in a macro group that defines custom commands for a custom menu bar to run a macro from the custom menu. However, it's easier to use the new command bars functionality to run a macro from a menu bar, toolbar, or shortcut menu. On the View menu, point to Toolbars and click Customize to display the Customize dialog box. On the Toolbars tab, select the menu bar, toolbar, or shortcut menu that you want to run the macro from. On the Commands tab, click All Macros in the Categories box, then in the Commands box select and drag the macro you want to run to the menu bar, toolbar, or shortcut menu. When you click the icon for this macro on the menu bar, toolbar, or shortcut menu, Microsoft Access will run the macro. You can also drag a macro from the Database window on to a menu bar or toolbar and Microsoft Access will create an icon that runs that macro.

To run the RunMacro action in Visual Basic, use the RunMacro method of the DoCmd object.

See Also

AddMenu action, RunMacro method.