What Is an Event

Events are triggered by actions taken by the user and the application, such as changes to data, mouse movements, or the opening of a form. You use VBA code or macros to respond to events. In other words, you link up the code you write with the user interface of your application through events. For example, you specify which VBA procedure or function to run when a particular event happens. This concept was illustrated briefly in Chapter 2 when you created new procedures and functions. Remember that you tied an event to a procedure so that the procedure would run. Let's look at some additional examples so this important concept will become totally clear.

One way to view a list of events available for a control is from the Properties dialog box that you saw earlier. As you recall, you can select View C> Properties from a designer, such as the form or report designer, to view the Properties dialog box. Events for form and report objects, such as controls, can be located by selecting the object and then viewing the Event tab of the Properties dialog box. Figure 3.6 shows the Event tab of the Properties dialog box for a form.

Form

Form v

Formal Data Event other All

On Current

Before Insert

After Insert

Before Update

A

After Update

On Dirty

On Undo

On Delete

Before Del Confirm

After Del Confirm

On Open

_

Figure 3.6

Notice how various types of events are listed. These events allow you to specify when the VBA code you write should execute. You actually have three options for how the event can execute. When you select a particular event from the list and click the Ellipses button (...), a dialog box appears that gives you the option to use the Expression Builder, the Macro Builder, or the Code Builder.

In all the examples so far, you have used the Code Builder because this is a book about VBA code. If you choose Expression Builder, the value displayed in the Properties dialog box will be "=expression", where expression is the expression that was entered in the Expression Builder window. If Macro Builder is chosen, the value is the name of the macro. If Code Builder is chosen, the value will be "[Event

Procedure]".

Like properties, various objects often have many or all the same events in common. For example, suppose you have a form with a text box named txtName (that is, with the Name property set to txtName) that you will use to display a message to the user any time the On Key Press event occurs. First select the txtName text box on the form, and then select the Event tab of the Properties dialog box for the text box. Because you want to create an event procedure, select the Event Procedure option from the list that is shown in Figure 3.7 and then click the Ellipses (...) button. If you click the Ellipses (...) button without selecting the Event Procedure option, you would be prompted to select Code Builder from the list of three options.

When you click the Ellipses button (...), the VBE opens with a new empty procedure created for the KeyPress event. Suppose the following code is added to the KeyPress event for the txtName text box object:

Private Sub txtName_KeyPress(KeyAscii As Integer) MsgBox "The KeyPress event just fired" End Sub

When the form runs and something is typed into the txtName field, the message in Figure 3.8 is displayed:

zJ] frmTestProperties Form

I ■ ■ ■ 1 ... 1 2 . .

_!T Text Box: txtName

n

I * Detail

txtNanne v

2 <

:::::B

«

Format Data Event other All

On Exit

On Got Focus

On Lost Focus

On Click

Q

a

:::::: Napei: ::::::::

: : : : : «Jnbound

On Mouse Down

On Mouse Move

On Mouse Up

On Key Down

On Key Up

On Key Press

Event Procedure 1 M

III!

Figure 3.7

Figure 3.8

Notice that the event fires before you see the value in the text box on the form. Other examples of events commonly used are On Click, On Double Click, and On Open. So many types of events are available for a particular object that it is impossible to cover them all in this chapter. However, I hope you understand that an event allows you to hook up your forms and reports with the VBA code that you write based on certain events that occur in the application. I present another example momentarily, just to make sure you are clear on the difference between properties, methods, and events.

Try It Out Using Properties, Methods, and Events

It's your turn now to try your hand at an example that uses properties, methods, and events. As you work, try to keep track of which items are properties, which are methods, and which are events. You can check your answers when I explain the example.

1. If you have not already done so, create a new database for use with examples in this chapter. To do so, select File C> New C> Blank Database. Specify the filename as Ch3CodeExamples and click the Create button.

2. Add a new form to the database. From the database window, select Forms, and then double-click the option Create Form in Design View. Alternatively, you can select the Forms object in the database window and then click the New button and choose Design View.

3. On the new form, use the Toolbox to drag and drop a text box control and a command button to the form, as shown in Figure 3.9. If the Toolbox is not displayed, select View C> Toolbox to display it.

Figure 3.9

4. Select the text box control. Select View C> Properties to view the Properties dialog box if it is not already displayed. From the Properties dialog box for the text box control, change the Name property to txtDateOfBirth.

5. Select the label. From the Properties dialog box, change the Name property of the label to lblDateOfBirth. Change the Caption property of the label to Enter Date of Birth:

6. Select the Command button. From the Properties dialog box, change the Name property of the Command button to cmdCalculate, and change the Caption property to Calculate. The form should now look like the one shown in Figure 3.10.

7. Click the Save button on the Toolbar and then specify the name frmTestObjects for the form.

Figure 3.10

8. Select the txtDateOfBirth text box again. Select the Event tab of the Properties dialog box, and locate the On Exit event. Select the Code Builder option to open VBE to write code for the event. One way to do so is to choose Event Procedure in the drop-down list and then click Ellipses (...). You can also select the Ellipses button and then choose Code Builder from the dialog box that appears. In either case, add the following code to the Exit event:

Private Sub txtDateOfBirth_Exit(Cancel As

Integer)

'if the user did not enter a date, then

'display an error and set the focus back

'to the date of birth text box control.

If Not IsDate(txtDateOfBirth.Text) Then

MsgBox "You must enter a date for the

Date of Birth field."

txtDateOfBirth.SetFocus

Cancel = True

End If

End Sub

9. Select the cmdCalculate command button. On the Event tab of the Properties dialog box, select the On Click event and add the following code to the newly created event procedure as shown here:

Private Sub cmdCalculate_Click()

'declare local variable for month of birth Dim intMonthOfBirth As Integer

'convert the value in the text box to a 'month and assign the value to the month

'of birth variable

intMonthOfBirth = DatePart("M",

. CDate(txtDateOfBirth))

'display a message to the user

regarding

'the month of birth

MsgBox "Month you were born: "

& intMonthOfBirth

End Sub

10. Select the Save button from the toolbar to save all the changes. Return to the frmTestObjects form and select the Save button to save all changes on the form. Run the form by clicking the View button on the toolbar.

11. Enter something in the text box other than a date (Figure 3.11).

Figure 3.11

12. Now, click the Calculate button. The message should be similar to the one shown in Figure 3.12.

Microsoft Office Acce&s £¿1

OK

3ate of Birth field

Figure 3.12

Figure 3.12

13. After you click the OK button, focus returns to the Date of Birth text box field. Type your date of birth (as shown in Figure 3.13).

Figure 3.13

14. Click the Calculate button. You should get a message box similar to the own shown in Figure 3.14.

Figure 3.14
0 0

Post a comment