Microsoft Access Training

Creating and Using Macros

Learn to automate Access tasks by creating macros, attaching them to events, setting values, and adding conditional logic.

Start here

Overview

This lesson creates a macro that opens a report, links it to a command button, and then builds an embedded macro that calculates return dates based on destination.

What You Will Learn

  • Create and save a standalone macro.
  • Use the OpenReport action.
  • Attach a macro to a command button's On Click event.
  • Create an embedded macro from a control event.
  • Use the SetValue action.
  • Create a conditional If block in a macro.
  • Add comments that document macro behavior.
  • Test macro actions from forms.

Before You Begin

  • Creating forms and reports
  • Form Design View
  • Calculated expressions
  • Basic event concepts

Why This Matters

Macros automate repeatable tasks and let forms respond to user actions without requiring VBA programming.

Plan Your Study

  • Create McrTripsReport with OpenReport.
  • Add a Preview Trip Report command button to FrmClients.
  • Connect the button's On Click event to the macro.
  • Create an After Update embedded macro in FrmTrips.
  • Use SetValue to calculate ReturnDate.
  • Add conditional logic for longer trips.
  • Test the completed automation.

Next step

Read the complete chapter.

The Learn tab contains the written lesson from Surviving Statistics.

Go to Learn

Written lesson

Learn Creating and Using Macros

Learn to automate Access tasks by creating macros, attaching them to events, setting values, and adding conditional logic.

Advertisement

Creating and Using Macros

Much of the power in an Access database comes when you intitally set it up. One of the essential elements of proper database design is understanding how to use Macros in Access. This lesson will step you through creating and using Macros.

Creating and Using Macros

Macros allow you to automate repetitive tasks. You can create a macro to run a particular report, or even to open a certain form and find a specific record. In this lesson, you will create a macro that previews a report and one that automatically sets a trip’s return date after a departure date is entered. This macro will set different trip lengths based on destination. Macros can be very useful in forms.

Macros execute when they are triggered by an event. An event might be clicking a “Run Report” button or, an event might be changing the value in a field.

Creating macros

Macros can be created by using the Event tab in the Property Sheet or the Macro tool on the ribbon’s Create tab. Either way, you must link the macro to the control and to the event you want to trigger the macro.

You will now create a macro that runs the trip report and then tie that macro to a button you will create in the clients form.

1. If not already open, open the No Fault Travel database. On the Create tab, click the Macro tool in the Macros & Code group.

The Macro Builder should open. Unlike other Microsoft Office programs, you do not record macros to create them in Access. Instead of recording them, you will build them action by action.

Microsoft Access lesson screenshot 8thLesson1

2. In the Add New Action dropdown list just below the Macro tab, click the dropdown arrow to open the list.

3. From the list of commands, choose OpenReport.

Microsoft Access lesson screenshot 8thLesson2

The OpenReport command will cause Access to open a report. In the next step, you will specify which report you want to open.

4. Click the dropdown list arrow in the Report Name text box and choose RptTripsByClients.

The next step is to specify how you want to view the report: in Design View, Report View, Print Preview, or in print (on paper).

Microsoft Access lesson screenshot 8thLesson3

5. Click the dropdown list arrow in the View text box and choose Print Preview.

When you run this macro it will open the report in Print Preview mode.

Microsoft Access lesson screenshot 8thLesson4

6. Save this macro as McrTripsReport and close it.

You may need to close both the macro and Macro Builder. The macro is now complete.

Linking an event to a macro

After creating the macro, the next step is to open the form that contains or will contain the control you want to link to this macro. To link a control to a macro, you use the control’s Event Properties. There are several events you can use to trigger a macro. In this portion of the lesson you will create a button in the clients form and then link the report macro to the button. The macro will run when you click the button you are about to create. The event you will tie to the macro is “on click”.

1. Open FrmClients in Design View.

Microsoft Access lesson screenshot 8thLesson5

2. In the Controls locate and click the Button tool.

Microsoft Access lesson screenshot 8thLesson6

3. Move the mouse pointer to an empty area in the Form Header section and click.

You will now see the Command Button Wizard dialog box. The Command Button Wizard would help you build Visual Basic code to automate operations like opening forms and reports. This feature is often easier to use than creating macros. The advantage of creating macros is that macros are easier to modify for specific operations if you are not a Visual Basic programmer.

You could easily use the Command Button Wizard to cause this button to open the same report, but by creating and linking macros, you are also learning a little bit about events. Experimenting on your own with the Wizard will help you learn its power and how easy it is to use.

4. Cancel the Command Button Wizard.

You will now see a button in this form. You will now change the text this button displays and link the macro you just created to this button.

5. Select the button you just created and display the property sheet for this control.

Microsoft Access lesson screenshot 8thLesson7

6. Click the Format tab on the Property Sheet. Change the Caption property to read Preview Trip Report.

The button will display the Caption property text. You will now tie the macro you created earlier to the On Click property of this button. You will do this on the Event tab,

7. Click the Event tab and then click in the On Click property.

Microsoft Access lesson screenshot 8thLesson8

8. Click the dropdown arrow in this row and select the macro McrTripsReport.

When you click the dropdown arrow, all existing macros will be listed so that you can use the same macro in multiple places if you wish. Notice there are other event you could tie this or other macros to for this button. Different types of controls will have different events that can be used to execute macros.

9. Close the Property Sheet. Save the form and view in Form View. Click the Preview Trip Report button and verify that the macro works correctly.

Microsoft Access lesson screenshot 8thLesson9

If needed, expand the Control button to display the entire text.

After clicking the button in the form you should be viewing the Trips report. If the macro does not work, go to the macros tab and open the macro. Make any needed corrections and try the process again.

10. Close Print Preview.

You should now be back in the clients form.

11. Close the clients form,

You will now create and link a macro that automatically sets the return date based in the departure date in the Trips Form.

The Set Value Action

The macro you are about to create will automatically set the return date once a departure date has been entered. We will assume that most trips are 10 days long. Our macro will use the set value action to set the return date equal to 10 days after the departure date.

The set value action allows you to enter expressions in equation arguments which will determine the value of the equation. In other words, the set value action will set the return date to a value equal to the departure date + 10.

1. Display FrmTrips in Design View.

Microsoft Access lesson screenshot 8thLesson10

2. Select the DepartureDate text box. Display the Event tab in the Property Sheet.

You will link the macro to the departure date field. After the user enters the departure date, the macro will set the return date in the return date field. In this exercise, you will create and tie the macro; in the previous exercise, the macro was created before you tied it to a control.

3. Click in the After Update row and then click the build (...)button.

Microsoft Access lesson screenshot 8thLesson11

4. In the Choose Builder dialog box, select Macro Builder and click OK.

Microsoft Access lesson screenshot 8thLesson12

5. In the Design tab, select the Show All Actions tool.

If this option is not selected, you will not be able to find the SetValue action that is needed for the next step.

Microsoft Access lesson screenshot 8thLesson13

6. Choose SetValue as the action from the dropdown menu.

You will now set the arguments for the SetValue action. You will specify for which control you want to set a value. In this case, you want this macro to set a value in the return date field.

7. In the Item row, type [ReturnDate].

Remember that Access always requires brackets when referencing field names.

Microsoft Access lesson screenshot 8thLesson14

8. In the Expression row, type [DepartureDate]+10.

The expression is the value you want to appear in the return date field. As with the other expressions you have created, field names are always enclosed in brackets.

By default when the user enters a departure date, a return date 10 days later than the departure date appears in the return date field.

Using conditions in macros

The macro schedules all trips as 10 days in length, but the return date can be changed by the data entry person. While the majority of trips scheduled by the No Fault Travel company are 10 days, trips to Hanoi or Paris are typically 15 days long. With the present macro, the user always has to change the return date for trips to Hanoi or Paris.

A more useful macro would use a 10-day trip length, unless the destination was Hanoi or Paris. If the case of those two destinations, the trip length would be set to 15 days. To accomplish this, you will have to add a condition to the macro to check the destination before setting the return date.

1. Ensure the Action Catalog is displayed. If not, click the Action Catalog tool in the Design tab on the ribbon.

Microsoft Access lesson screenshot 8thLesson15

The Action Catalog allows you to include comments for documentation or to add conditions to your macros. In this portion of the lesson, you will add a condition which changes some return dates to 15 days after departure, depending on the destination. You will also add documentation to each macro step.

2. Under Program Flow in the Action Catalog pane, double click Comment.

A text area should appear where you can describe what this macro step does.

Microsoft Access lesson screenshot 8thLesson16

3. In the comment area, type: This sets the return date of all trips, except Hanoi and Paris, to 10 days after the departure date. Hanoi and Paris are set as 15 day trips.

The next step is to add the condition by selecting the If option under Program Flow. The condition will be dependent on the trip destination field, DestinationID.

In an earlier lesson, you created the DestinationID field as a lookup field on the trips table which lists the destination from the destination table. Although the destination dropdown list hides the destination ID, the DestinationID field actually stores the identification number for each destination. Therefore, the condition you will use to select Hanoi will be [DestinationId]=1 not [DestinationId]=”Hanoi”.

The table TblDestinations below shows the respective identification numbers for each destination. To prove to yourself that the DestinationID field in TblTrips and FrmTrips is a number, open TblTrips in Design View and notice that the DestinationID field (which is entered, modified and viewed in FrmTrips) has a number data type.

Microsoft Access lesson screenshot 8thLesson17

4. Under Program Flow in the Action Catalog pane, double click If.

You should now see that Access has added rows to enter the condition and the action to take if the condition is met.

5. In the If row, type: [DestinationID]=1 Or [DestinationID]=6.

Microsoft Access lesson screenshot 8thLesson18

6. Click in the “Add New Action” box and select SetValue. Enter [ReturnDate] in the Item row and [DepartureDate]+15 in the Expression row.

Microsoft Access lesson screenshot 8thLesson19

7. On the Design tab, click the Save tool to save this macro. Close the Action Catalog and macro after saving.

You are now ready to test this macro.

Microsoft Access lesson screenshot 8thLesson20

8. Save the trips form and display it in Form View. Add the two new trips shown above.

As soon as you enter the departure date for the two new trips, the return date field should auto populate as 10 to 15 days after departure, depending on the destination.

9. Close the trips form, FrmTrips.

Advertisement

Next step

See the concepts demonstrated.

Continue to Watch for chapter videos and worked examples.

Go to Watch

See it explained

Watch

Choose the explanation or example that best supports your learning.

Instructional videos will be added later.

Next step

Apply what you have learned.

Use the guided questions and activities in Practice.

Go to Practice

Apply what you learned

Practice Creating and Using Macros

Automate report preview and return-date entry in the No Fault Travel database.

Guided Practice

  • Create McrTripsReport with the OpenReport action.
  • Set the report to open in Print Preview.
  • Add a command button to FrmClients and set its caption.
  • Attach the button's On Click event to McrTripsReport.
  • Create an After Update macro for DepartureDate.
  • Set ReturnDate to DepartureDate plus 10 days.
  • Add an If condition that uses 15 days for the specified destinations.
  • Save and test all macro actions.

Challenge Yourself

  • Create a second button that opens another report or form.
  • Add an Else branch for a different destination rule.
  • Document each macro with comments.
  • Explain when an embedded macro is preferable to a standalone macro.

Check Your Understanding

  • Can you distinguish an action, event, and condition?
  • Can you explain what SetValue changes?
  • Can you identify the difference between a standalone and embedded macro?
  • Can you explain why the After Update event is appropriate here?

Next step

Save the materials you may need later.

Open Resources for the study sheet, spreadsheets, and related learning materials.

Go to Resources

Keep learning

Resources

Study sheets, downloadable files, companion tools, and related lessons for this chapter.

For educators

Teaching this chapter?

The Instructor tab contains objectives, teaching ideas, assessments, and a printable guide.

Open Instructor Resources

LutherMaddy.com — Helping Humans Learn

Creating and Using Macros: Instructor Resources

Teach this lesson

Instructor Resources

Classroom-ready objectives, teaching ideas, questions, and assessment materials for this chapter.

Learning Objectives

  • Create and save a standalone Access macro.
  • Use OpenReport to preview a report.
  • Attach a macro to a form control event.
  • Create an embedded macro.
  • Use SetValue to change a control value.
  • Add conditional logic and comments.
  • Test and troubleshoot macro behavior.

Prerequisite Knowledge

  • Forms
  • Reports
  • Design View
  • Calculated expressions

Teaching Outline

  • Create McrTripsReport.
  • Add the report-preview button.
  • Connect the On Click event.
  • Create the DepartureDate After Update macro.
  • Use SetValue.
  • Add the destination condition.
  • Test and document the macro.

Key Concepts

  • Macros contain one or more actions.
  • Events determine when macros run.
  • Embedded macros belong to a specific form or control.
  • SetValue changes a control or field value.
  • If blocks run actions only when a condition is true.
  • Comments document automation logic.

Classroom Demonstration

  • Click the report button and trace the event to the macro action.
  • Change a destination and show the conditional return date.
  • Temporarily alter the condition to demonstrate troubleshooting.

Questions to Ask

  • When is a macro preferable to a manual series of actions?
  • Why is After Update better than On Current for this calculation?
  • What advantages do comments provide?

Teaching Notes

  • Test each action before adding the next.
  • Use Show All Actions carefully and explain database trust settings.
  • Distinguish macros from VBA code.

Assessment Ideas

  • Create a standalone report macro.
  • Attach it to a form button.
  • Create a conditional embedded macro.
  • Explain the event and each action.

Grading Rubric

CriterionPoints
Standalone macro correct2
Button and event connection correct2
SetValue action correct2
Conditional logic correct2
Macro tested and documented2
Total10

Exam and Discussion Questions

Basic

  • Define macro action, event, condition, and embedded macro.

Applied

  • Create a button that previews a specified report.

Conceptual

  • Explain why a conditional macro may be preferable to several separate macros.

Keep Exploring

Continue Learning

Explore step-by-step lessons, interactive calculators, downloadable study sheets, and practical educational resources.