You can use the StopMacro action to stop the currently running macro (macro: An action or set of actions that you can use to automate tasks.).
The StopMacro action doesn't have any arguments.
You typically use this action when a condition makes it necessary to stop the macro. You can use a conditional expression (conditional expression: An expression evaluated and compared to a value; for example, If...Then and Select Case statements. If the condition is met, one or more operations are performed. If it isn't met, the operation is skipped.) in the macro's 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.) that contains this action. When the expression evaluates to True (–1), Microsoft Office Access 2007 stops the macro.
For example, you might create a macro that opens a form (form: An Access database object on which you place controls for taking actions or for entering, displaying, and editing data in fields.) showing the daily order totals for the date entered in a custom dialog box (custom dialog box: A modal pop-up form you create to ask the user for additional information or to display a message.). You could use a conditional expression to be sure that the Order Date 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 the dialog box contains a valid date. If it doesn't, the MsgBox action can display an error message and the StopMacro action can stop the macro.
If the macro has used the Echo or SetWarnings actions to turn echo (echo: The process of Access updating or repainting the screen while a macro is running.) or the display of system messages off, the StopMacro action automatically turns them back on.
This action isn't available in a Visual Basic for Applications (VBA) module.
Synchronize forms by using a macro
The following macro opens a Product List form in the lower-right corner of the Suppliers form, displaying the current supplier's products. It shows the use of the Echo, MsgBox, GoToControl, StopMacro, OpenForm, and MoveSize actions. It also shows the use of a conditional expression with the MsgBox, GoToControl, and StopMacro actions. This macro should be attached to the Review Products button on the Suppliers form.
||Echo On: No
||Stop screen updating while the macro is running.
Message: Move to the supplier record whose products you want to see, then click the Review Products button again.
Title: Select a Supplier
|If there is no current supplier on the Suppliers form, display a message.
||Control Name: CompanyName
||Move focus to the CompanyName control.
||Stop the macro.
Form Name: Product List
Where Condition: [SupplierID] = [Forms]![Suppliers]![SupplierID]
Data Mode: Read Only
Window Mode: Normal
|Open the Product List form and show the current supplier's products.
|Position the Product List form in the lower right of the Suppliers form.