Lesson10.4.Assigning a Macro to an Event


Lesson 10.4. Assigning a Macro to an Event

Figure 10-6. Adding a command button to run a macro.


Figure 10-7. Assigning a macro to a command button's "On Click" 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.

  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 10-6. 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 10-1.

  6. Click the On Click box, click the list arrow, and select mcrSales, as shown in Figure 10-7. 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.

Table 10-1. Event Properties That Can Trigger Macros

Event

Description

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.


QUICK REFERENCE

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.




CustomGuide Inc - Access 2003 Personal Trainer
Access 2003 Personal Trainer (Personal Trainer (OReilly))
ISBN: 0596009372
EAN: 2147483647
Year: 2005
Pages: 209

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net