Access Cookbook - Ken Getz [8]
TIP
For more information on attaching macros to events, see macros; creating in Access online help.
How Do I Create an Event Procedure?
Programming in Access often depends on having VBA procedures react to events that occur as you interact with forms or reports. To create a VBA procedure that will react to a user event, follow these steps:
Select the appropriate object (report, form, or control) and make sure the properties window is displayed.
Choose the Event Properties page on the properties window, or just scroll down the list until you find the event property you need.
Select the property, then click the down arrow button next to the property. Select [Event Procedure] from the list of options.
Click the "..." button to the right of the event name, as shown in Figure P-7. This is the Build button, and it appears next to properties window items that have associated builders. In this case, clicking the Build button takes you to a stub for the event procedure you need to create.
Figure P-7. Press the Build button to invoke the Choose Builder dialog
PROPERTY NAMES VERSUS EVENT NAMES
The naming of event properties, as opposed to the events themselves, is rather ambiguous in Access. The event properties, in general, have an "On" prefix, as in "OnClick" or "OnActivate." The events themselves, however, are named without the "On" prefix, as in "the Click event" or "the Activate event." We've tried to be consistent throughout the book, but there are some places where the context just doesn't indicate which is the correct usage. You'll need to be aware that with or without the "On" prefix, when the event occurs, it activates the procedure whose name is listed in the properties window for that event.
When you create a new event procedure, Access creates the subroutine name, fills in the parameters that it passes, and places the subroutine into the form or report's class module. The name of the procedure is always the name of the object, followed by an underscore and the name of the event. For example, had you created the Click event procedure for the cmdClose command button, you'd see a code skeleton like this:
Sub cmdClose_Click( )
End Sub
Now follow these steps to complete the process:
If the solution asks you to enter code into the event procedure, enter it between the lines of code that Access has created for you. Usually, the code example in the solution will include the Sub and End Sub statements, so don't enter them again.
When you're done, close the module window and save the form. By saving the form or report, you also save the form's module.
How Do I Place Code in a Form or Report's Module?
When a solution asks you to place a procedure in a form or report's module that isn't directly called from an event, follow these simple steps:
With the form or report open in design mode, choose View → Code, press F7, or click on the Code button on the toolbar, as shown in Figure P-8.
Figure P-8. Click on the Code toolbar button to view a form or report's module
To create a new procedure, follow the steps in How Do I Create a New Module?, starting at Step 3.
Choose File → Save, close the module, then save the form, or just click on the Save icon on the toolbar.
How Do I Know What to Do with Code Examples?
In most cases, the solutions suggest that you import a module (or multiple modules) from the sample database for the particular solution, rather than typing in code yourself. In fact, code that isn't referenced as part of the discussion doesn't show up at all in the body of the solution. Therefore, you should count on importing modules as directed. Then follow the instructions in each solution to finish working with and studying the code.
If the solution tells you to place some code in a form's module, follow the steps in How Do I Place Code in a Form or Report's Module?. If you are instructed to place code in a global module, follow the steps in How Do I Create a New Module?. In most cases, you'll just import an existing module and won't type anything at