Associating Code to an Event Property

When you are ready to begin associating VBA code to an event property, this is how you instruct Access to use your VBA code: You can start from either the Design View of the Report or from the VBA Editor window itself. Both methods are useful, and the times at which you utilize them will depend on your current location (Report or Editor) at the time that you need to start a new procedure.

From the Report Design window, click to highlight the desired control, and then choose View/Properties from the main menu. (You can also press F4 or just double-click the control to open the Properties window.) Once opened, click the Event tab, open the combo box associated to the desired event property, and choose [Event Procedure], as shown in Figure 11-2. Performing this step signifies to Access that you want to execute a VBA event procedure when the user triggers the event. Notice in Figure 11-2 that a macro can also be assigned to an event property. This is why there is a selection for event procedure. The reason it is in square brackets is that it will always be the first item in the combo box.

To speed the selection of [Event Procedure] when you enter the combo box of an event property, press the Left Square Bracket key "[", or just double-click the combo box. (Double-clicking a second time will select the next value in the list.)

Figure 11-2

After the selection of [Event Procedure], click the button that is just to the right of the combo box "opener" (the button has the Period of Ellipses [...] on it). This action will cause the VBA Editor window to open. When the event procedure for the event property is opened for the first time, a new sub procedure is created for it. If there already exists a sub procedure for the report or control event property, then that existing sub procedure is opened.

From the Module window itself, use the combo boxes at the top of the Code window to select the desired control, and then the subsequent event property. The combo box on the left has a value for the report, and all controls on the report. The combo box on the right has all of the possible event procedures based on the selection from the left combo box.

Figure 11-3 shows the Control and Event Property combo boxes, as well as a sample event procedure structure that is created for you. Notice that the procedure is a Sub, which means that it cannot return a value. It is also dimensioned as Private, which means that other modules in the database cannot see nor utilize it. The last thing to notice in the picture is that the sub procedure has the Cancel parameter. The use of the Cancel parameter is described later in this chapter.

Note: Using a naming convention comes in handy when viewing the Control combo box in the VBA Editor. All controls are listed alphabetically, so voluntarily adding a prefix to each control name will sort each control type together (that is, txt, cbo, cmd, and so on).



6ef oseUpclaie




e user, cion't displav an □ 1 Then











Private Sub ShipMaire BeioreTJpdate (Cancel As Integer)

End Sub

Figure 11-3

0 0

Post a comment