MS Access

Working with Macro Groups

If you are creating lots of macros, you might want to consider organizing them into a macro group to help you manage them. A macro group stores several related macros together in a single macro object.

When you create a macro group, you must give each macro in the macro group its own unique name to identify where each macro starts and ends. You do this by entering the macro names in the Macro Name column, which you display by clicking the Macro Names button on the toolbar.

Two macros the Open frmEmployees macro and the Print rptEmployeeSales macro within a single macro group.
The Run Macro dialog box.

When you combine two or more macros within the same macro group, you must run them separately, by referring to the macro group name, followed by the macro name. For example, mcrEmployees. mcrEmployees frmEmployees refers to the Open frmEmployees macro in the mcrEmployees macro group.

In this lesson you will learn how to group several related macros together in a macro group.

  1. Select the mcrEmployees macro and click the Design button.

    In order to work with macro , you need to display the Macro Name column.

  2. Click the Macro Names button on the toolbar.

    First you need to give the macro you created in the previous two lessons a name. The cursor is already positioned in the Macro Name cell of the first row.

    Always enter the macro name in the Macro Name column, next to the Action where the macro starts.

  3. Type Open frmEmployees in the first blank cell in the Macro Name column.

    The macro name "Open frmEmployees" identifies the macro you created in the previous two lessons. To create another macro in the same macro group, type its name in the Macro Name column next to the first action of the new macro.

  4. Press the key three times.

    The cursor should be positioned in the Macro Name column next to the first blank Action row. This is where you will add another macro to the macro group.

  5. Type Print rptEmployeeSales.

    "Print rptEmployeeSales" is the name of the new macro we will create.

  6. Click the Action cell to the right of the Print rptEmployeeSales macro name, click the list arrow, scroll down, and select OpenReport.

    Similar to the OpenForm action, which opens a form, the OpenReport action opens a report. Next, you need to specify the arguments for the OpenReport action.

  7. Click the Report Name text box in the Action Arguments panel, click the list arrow, and select rptEmployeeSales.

    This macro will open the rptEmployeeSales report. Notice Print appears in the View argument boxthis will send the rptEmployeeSales report directly to the printer. Let's add a comment to this new macro.

  8. Click the blank Comment box in the Print rptEmployeeSales macro row and type This macro prints the rptEmployeeSales report.

    You want to add one more action to the Print rptEmployeeSales macro.

  9. Click the Action cell just below the OpenReport action, click the list arrow, scroll down, and select MsgBox.

    You need to tell Access what you want the message box to say.

  10. Click the Message argument box and type The Employee Sales report has been sent to the printer.

    Let's specify several additional arguments for the MsgBox action, such as the type of icon that is displayed in the message box.

  11. Click the Type argument box, click the list arrow, and select Information. Click the Title argument box and type Notice.

    Your macro should look like the one shown in figure.

  12. Click the Save button on the toolbar and close the macro window.

    Let's test our new macro. When you combine several macros within the same macro group you must run them separately using the Tools command on the Access menu.

    Don't run a macro group by double-clicking it or selecting it and clicking Run. Doing so will run every macro in the macro groupoften with disastrous results!

  13. Select Tools » Macro » Run Macro from the menu.

    The Run Macro dialog box appears, as shown in figure. Here's where you select the specific macro you want to run.

  14. Click the Macro Name list arrow, select mcrEmployees.Open frmEmployees, and click OK.

    Access runs the Open frmEmployees macro.

  15. Click OK to close the message box and close the frmEmployees form.

If you want, go ahead and repeat Step 13 and run the Print rptEmployeeSales macro. Make sure your computer is connected to a printer, as this macro will send a copy of the rptEmployeeSales report to the printer.

TO CREATE A MACRO GROUP:

  1. CREATE A NEW MACRO OR EDIT AN EXISTING MACRO.

  2. CLICK THE MACRO NAMES BUTTON ON THE TOOLBAR.

  3. TYPE THE MACRO NAME IN THE MACRO NAME COLUMN NEXT TO THE ACTION WHERE THE MACRO STARTS.

  4. IF NECESSARY, ADD THE MACRO ACTIONS OR EDIT THE EXISTING MACRO ACTIONS.

  5. SAVE THE MACRO AND CLOSE THE MACRO WINDOW.

TO RUN A MACRO IN A MACRO GROUP:

  1. SELECT TOOLS » MACRO » RUN MACRO FROM THE MENU.

  2. CLICK THE MACRO NAME LIST ARROW, SELECT THE MACRO YOU WANT TO RUN, AND CLICK OK.