MS-Access / Getting Started

Creating a Macro

The first step in creating a macro is to design the macro carefully by listing the actions you want performed when the event occurs. Each action might require specific arguments or need to be performed only under certain conditions.

Next, verify that you're choosing the correct event to which the macro will respond.

When you complete the planning and design of the macro action list, you're ready to open the Macro window. You have two ways to start the Macro Builder:

  • On the Create tab's Other group, click the New Object: Macro command.
  • Go to the Property Sheet for the control that needs the macro, click the Build button in the Event property box, and choose the Macro Builder in the Choose Builder dialog box.

Touring the Macro Design Window

The Macro Design window opens, showing a blank macro. The drop-down list in the Action column contains a list of actions from which to choose. The Arguments column displays a list of arguments to select for each macro action. As you enter arguments in the lower pane, they are listed in the Arguments column. Entries in the Comment column are optional but highly recommended as a reminder of what the macro is meant to accomplish.

TIP: Comments are especially useful when macros are stored as separate objects, not embedded with a particular form or report. The comments can explain how the macro is used and to which events it's attached. This can also be important if you rename the macro. You need to find all the references to the macro and change the name there, as well.

Once you select an action from the list, the lower pane displays the associated arguments. Some are required, while others are optional, depending on the action.

The Macro Tools ribbon includes some new groups and commands:

In the Tools group:

  • The Run command runs the macro.
  • The Single Step command runs the macro one action at a time and displays intermediate information.
  • The Builder command starts the Expression Builder for help in creating an expression.

In the Rows group:

  • The Insert Rows command inserts one or more blank rows in the grid above the selected row. If you select more than one row, that number of rows is inserted. You can also right-click in the macro design and choose Insert Rows from the shortcut menu.
  • The Delete Rows command deletes the selected row or rows. You can also choose this command from the shortcut menu.

In the Show/Hide group:

  • The Show All Actions command expands the list of actions to include those that may require trusted status. If the command is dimmed, the only macro actions available for use are those that do not require trusted status to run.
  • The Macro Names command shows or hides the Macro Name column in the macro design.
  • The Conditions command shows or hides the Condition column in the macro design.
  • The Arguments command shows or hides the Arguments column in the macro design.

The lower pane is the Action Arguments pane, and it displays information about the current row of the macro. The lower-right pane in the Macro window displays information about the currently active part of the macro sheet. For example, the first row is selected and the message "Enter an action in this column" is displayed in the information pane. As you work in the macro sheet, the pane shows other information and comments.

Creating a Simple Macro

Here you'll create a macro in the Police database that opens the Alpha Card form in readonly mode by doing the following:

  1. Open the Macro window from the Create tab or start the Query Builder from the Property Sheet. The Macro window appears.
  2. In the first row of the macro, click the Action drop-down arrow and choose OpenForm from the list.
    The Action Arguments pane now contains the arguments for the OpenForm action and the information pane describes the selected OpenForm action. The required arguments are also displayed in the Arguments column. The Form Name argument is required. Other required arguments show default selections. The optional arguments are blank in the Action Arguments pane and skipped in the Arguments column.
    NOTE: You can remove the Arguments column by clicking the Arguments command in the Show/Hide group.
  3. Click the Form Name box in the Action Arguments pane and select the Alpha Card form from the list of the forms in the current database.
  4. Set the other arguments as follows:
    • View (required) Choose from the list of options including Form (default), Design, Print Preview, Datasheet, PivotTable, or PivotChart. For this example, leave the default setting as Form view.
    • Filter Name (optional) Enter the name of the filter to limit or sort the records in the form. This can be a query or a filter saved as a query. For now, leave this blank because you want all the records.
    • Where Condition (optional) Enter a SQL WHERE clause that limits the records in the form. You can click the Build button next to the argument box to start the Expression Builder if you need help. Again, you want all the records with this example, so don't add a Where Condition.
    • Data Mode (optional) Choose from the list of data entry modes: Add, to allow adding new records; Edit, to allow editing of existing records; or Read Only, to prevent any additions or editing. Choose Read Only from the list for this example.
    • Window Mode (required) Choose from the list of window modes, including Normal (default); Hidden, which hides the form; Icon, which displays the form minimized; or Dialog, which sets the form Popup and Modal properties both to Yes. Leave the Window Mode with the default Normal setting for now.
  5. If you started the Macro Builder from the Create tab, close the Macro window, enter a name for the macro in the Save As dialog box, and then click OK. When you return to the Macros group in the Navigation Pane, you'll see the name of the new macro. If you started the Macro Builder from the Property Sheet, you will not be asked to name the macro. It is saved with the form or report design.
[Previous] [Contents] [Next]