Access offers actions that cover data management activities, such as opening forms and reports, printing reports, filtering data, validating data, moving among records in a form, playing sounds, displaying message boxes, and even exchanging data with other programs.
To add an action to a macro, you can either choose an action from the Action drop-down list or type the name yourself. As with many lists offered by Access for choosing an item, if you begin to type the name of the action, Access automatically fills in the remaining characters for you.
- To navigate between controls or records, use the FindNext, FindRecord, GoToControl, or GoToRecord actions.
- To modify an Access window, use the Maximize, Minimize, MoveSize, or Restore actions.
- To open an object, use the OpenForm, OpenModule, OpenQuery, OpenReport, or OpenTable actions.
- To set the value of a field or control, or a property of a form, control, or report, use the SetValue or SetProperty actions.
- To print a report, use the OpenReport action and set the View argument to Print Preview. If you want to print the report without previewing, set the View argument to Print.
- To run a query, use the OpenQuery action and set the View argument to Datasheet.
NOTE: If you don't see SetValue in the list of macro actions, click the Show All Actions command in the Show/Hide group on the Design tab.
Setting Action Arguments
Most macro actions have a list of associated arguments that give Access more information about how you want to carry out the action. Some arguments are required and others are optional. When you add an action to a macro, the argument list appears in the Action Arguments pane and the Arguments column.
You can usually type the value that you want in the field next to the argument, but many fields offer drop-down lists. If you enter a value, some arguments require that the value be included in the list. A description of the current argument is displayed in the information pane to the right of the argument list. If you need more help, press f1 with the insertion point in the argument field.
TIP: In some cases, choices for one argument can determine which choices are available for an argument farther down in the list. For this reason, setting the arguments in the order that they're listed in the Action Arguments pane is best.
If the argument requires an object name, you can enter the name or drag the object from the Navigation Pane to the argument field. When you drag an object from the Navigation Pane, Access automatically sets the appropriate arguments for that action.
Instead of selecting from a list or entering a value, you can enter an expression that evaluates to the argument value that you want to use. Always precede the expression with an equal sign (=) so Access recognizes it as an expression instead of an identifier. For example, the expression = [EntryNo] sets the argument to the value in the EntryNo control.
TIP: The equal sign rule has two exceptions: The Expression argument of the SetValue action and the RepeatExpression argument of the RunMacro action give unexpected results if you use an equal sign. They evaluate the expression twice.
If you want help from the Expression Builder, click the Build button, which appears at the right of the argument field when you select an argument that accepts an expression. Not all arguments accept expressions. For example, you must select from the list for the ObjectType argument. If you use an expression where one isn't permitted, you'll get an error message.
NOTE: If you want to refer to a control on another form, you must use the full object identifier syntax: [Forms]![formname]![controlname]. The ! symbol indicates what follows is an object named by the user. If the reference is to something named by Access such as a control property, you would use a period (.) to separate the object names. In addition, both forms must be open.
In this tutorial:
- Automating with Macros
- Standalone vs. Embedded Macros
- Creating a Macro
- Choosing Actions
- Testing and Debugging a Macro
- Adding Conditions to a Macro
- Assigning a Macro to an Event Property
- Some Common Uses for Macros
- Filtering Records
- Changing the Flow of Operations
- Create a Macro Group
- Documenting Macros
- Creating an AutoExec Macro