MS Access

Creating and Running a Macro

In some programs, such as Microsoft Excel or Word, you can create macros with a "macro recorder" to record your commands, keystrokes, and mouse clicks. Unfortunately, there isn't a "macro recorder" or Macro Wizard to help you create a macro in Microsoft Access.

Instead, you create macros by entering the actions and arguments directly in Macro Design view, shown in figure. Don't worry it's not as difficult as it sounds. Working in Macro Design view really isn't all that different from working in Table Design view it's where you define and edit your macro objects.

You create macros

Simple macros that automate a single task, such as opening a form or report, are incredibly simple to create we'll create such a macro in this lesson. More complicated macros with several steps or procedures may require a little bit of planning. Before you create a complicated macro, think about what you want the macro to do and the individual actions that are required to complete this operation. Practice the steps needed to carry out the operation and write them down as you go it will make writing the macro a lot easier.

And so, without any further ado, let's create our first macro.

  1. Start Microsoft Access, open the Lesson 10 database, click the Macros icon in the Objects bar, and click the New button.

    The Macro1: Macro window appears, as shown in figure. The Action cell is where you tell Access what you want the macro to do.

  2. Click the first blank Action cell, then click the list arrow.

    A list of actions appears. An action, or command, is the basic building block of a macro it's an instruction that tells Access what you want the macro to do. There are more than 50 different actions you can choose from. When you start creating your own macros you will almost certainly want to refer to Macro Action References (found in Assigning A Macro To a Keystroke Combination) to help you find the right action.

  3. Scroll down and select the OpenForm action.

    The OpenForm action is added to the first line of the macro window. Most of the time you will have to give Access more information about how to execute each action. For example, here we will have to tell Access which form to open with the OpenForm action. You use arguments to supply Microsoft Access with information about how to carry out the action. Each type of action has its own set of arguments, which appear in the Action Arguments panel, located at the bottom of the macro window.

  4. Click the Form Name text box in the Action Arguments panel, click the list arrow, and select frmEmployees.

    That's the only argument we need to specify for this exercise, but notice that there are additional arguments for the OpenForm action, such as the View argument, which lets you select the view in which to open the form (Form view, Design view, or Print Preview).

    If you want, you can type a comment to explain the action in the Comment column. If you've ever had any programming experience, the Comment column is the same as a remark statement.

  5. Click the first blank Comment cell and type This macro opens the frmEmployees Form.

    Comments are completely optional, but they do make your macros easier to understand, especially if other users will edit them.

  6. Click the Save button on the toolbar, save the macro as mcrEmployees and click OK.

    You're finished working in the Macro window for now so...

  7. Close the Macro window.

    Time to test your new macro. The Macros icon in the Objects bar in the Database window should be selected.

  8. Double-click the mcrEmployees macro.

    Access runs the mcrEmployees macro and opens the frmEmployees form.

  9. Close the frmEmployees form.

That's all there is to a macro not nearly as hard as you thought it would be, was it? Even the Microsoft Access developers don't have all the macro actions memorized, so make sure you have Macro Action Reference handy so that you know what action to use in your macros.

To create a macro:

  1. in the database window, click the macros icon in the objects bar and click the new button.

  2. click the first blank action cell.

  3. click the list arrow and select the action you want the macro to perform.

  4. specify any required arguments for the action in the action arguments area.

  5. repeat steps 2-4 for each additional action you want the macro to execute.

  6. click the save button on the toolbar, give your new macro a name, and click ok.

  7. close the macro window.

to run a macro:

  • click the macros icon in the objects bar and double-click the macro you want to run.