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.
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.
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.
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.
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.
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.
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.
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...
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. |