MS-Access / Getting Started

Assigning a Macro to an Event Property

Access responds to all kinds of events that occur when you're working with a form or report, including mouse clicks, changes in data, changes in focus, and opening or closing a form or report. Assigning the macro to the right event in a sequence is important. If you have not embedded the macro in an event property, you need to decide when you want the macro to run.

After you decide when you want the macro to run, you set the corresponding event property of the form, report, or control to the name of the macro. For example, if you want to run a macro that sounds a beep when a form opens, assign the macro to the On Click property of a control on a form.

To attach a macro to an event property, do the following:

  1. Open the form or report in Design or Layout view, and then select the form, report, section, or control to which you want to attach the macro.
  2. Open the Property Sheet and click the Event tab to see a list of events that can occur for the selected object.
  3. Click the property whose event you want to run the macro and choose the macro name from the drop-down list.
  4. Save and close the form or report design.

NOTE: If you select a macro name and then click Build (...) next to the property box, you open the macro Design window, where you can view and edit the selected macro.

When the event occurs, the built-in response, if any, occurs first, and then the macro runs. For example, when you click a button, the built-in response occurs and the button appears pressed. If you attach a macro to the On Click event property, the macro runs next.

TIP: If you want the macro to occur when you click either the control or its label, select them both before opening the Property Sheet. Then choose the macro name in the On Click event property for the multiple selection.

Deciding Which Event to Use

Although the Property Sheet shows quite a long list of event properties for forms and controls, you'll use a few of them more often than others. Here are some of the commonly used form and control event properties:

  • On Open: Occurs when the form opens, but before the first record is displayed. Use this property to open, close, or minimize other forms or to maximize this form.
  • On Current: Occurs when the form is opened and focus moves to a record or the form is refreshed or requeried. Use this to synchronize data among forms or to move focus to a specific control.
  • Before Update: Occurs after focus leaves a record, but before the data is saved in the database (also after a control loses focus, but before the control is changed). Use to display a message to confirm the change before it's completed.
  • After Update: Occurs after the record changes are saved in the database (also after a control loses focus and after the control is changed). Use to update the data in other controls, forms, or reports or move focus to a different page, control, or record in the form.
  • On Click: Occurs when you press and release the left mouse button over a control.
  • On Enter Occurs when you move to a control, but before it gets focus. Use to display information about data to enter in the control or a request for user password.

Reports and report sections have fewer event properties because little user interaction occurs with a report. Some of the more common event properties used to attach macros and event procedures to reports are as follows:

  • On No Data: Runs a macro when the report has an empty underlying recordset. Use to cancel the print event.
  • On Open: Runs a macro when the report opens, but before printing begins. Use to prompt for a filter for the records to be included in the report.
  • On Page: Runs a macro after the page is formatted, but before printing begins. Use to add a graphic or border design to the report.

Report sections also have a few event properties to which you can attach a macro or event procedure.

[Previous] [Contents] [Next]