MS Access

Assigning a Macro to an Event

Running macros from the Database window or menu is a pain in the neck. That's why most database developers assign macros to controlsparticularly, buttonsso that when a user clicks the button or control, a macro is activated.

Adding a command button to run a macro.

Assigning a macro to a command button's "On Click" event.

  1. Open the frmEmployees form in Design view.

    You want to add a command button to the frmEmployees form to open a report that displays the sales for each employee. First you need to add the command button.

  2. Click the Toolbox button, if necessary, and click the Command Button button on the Toolbox.

    The mouse pointer changes to a , indicating that you can click and drag the command button control onto the form.

  3. Place the pointer below the DOB field label and click and drag the pointer down and to the right to create a command button like the one in Figure. Click Cancel if the Command Button Wizard appears.

    Let's give this button a more meaningful text label.

  4. Make sure that the command button is still selected, then click its text label and replace the text with Sales Report. Click anywhere outside of the command button when you're finished.

    We're ready to assign a macro to the buttonto do this you will need to display the command button's Properties.

  5. Select the command button, click the Properties button on the toolbar, and click the Event tab.

    The lists all the events to which you can assign a macromost of them you will never use, as you can see in table.

  6. Click the On Click box, click the list arrow, and select mcrSales, as shown in figure. Close the Properties dialog box when you're finished.

    Let's see how our new command button works.

  7. Click the View button on the toolbar to switch to Form view, then click the new Sales Report button.

    Microsoft Access runs the mcrSales macro and displays the Employee Commission Report for the current employee. Let's close the report and save our changes...

  8. Close the Employee Commission Report and then click the Save button.

Event Properties That Can Trigger Macros

Before Update

Macro or function that runs when data in a field or record is changed but before the changes are actually saved to the database. Often used to validate data.

After Update

Macro or function that runs when data in a field or record is changed and is saved to the database.

On Change

Macro or function that runs when the contents of a text box or combo box changes or when you move from one page to another page in a tab control.

On Enter

Macro or function that runs when a control first gets the focus (is selected). The Enter event occurs before the focus moves to a particular control (before the GotFocus event). You can use an Enter macro or event procedure to display instructions when a form or report first opens.

On Exit

Macro or function that runs when a control loses focus (is deselected) on the same form.

On Got Focus

Macro or function that runs when a control gets the focus (is selected).

On Lost Focus

Macro or function that runs when a control loses the focus (is deselected).

On Click

Macro or function that runs when a control is clicked.

On Dbl Click

Macro or function that runs when a control is double-clicked.

On Mouse Down

Macro or function that runs when the user presses the mouse button.

On Mouse Move

Macro or function that runs when the user moves the mouse over a control.

On Mouse Up

Macro or function that runs when the user releases the mouse button.

On Key Down

Macro or function that runs when the user presses a key on the keyboard.

On Key Up

Macro or function that runs when the user releases a key on the keyboard.

On Key Press

Macro or function that runs when the user presses an ANSI key on the keyboard.

How to assign a macro to a control on a form or report:

  1. open the form or report in design view.

  2. click the control to which you want to assign the macro and click the properties button on the toolbar.

  3. click the event tab and click in the box for the type of event you want to assign to the macro.

  4. click the list arrow and select the macro you want to assign to the event.

  5. close the properties dialog box and save the form or report.

by BrainBellupdated
Advertisement: