HandsOn Writing an Event Procedure

1. Open the Acc2003_Chap01.mdb database file from the book's downloadable file. This file contains a copy of the Customers table and the Customers form from the Northwind database that comes with Microsoft Office Access.

2. Open the Customers form in Design view.

3. Right-click the ContactName text box control on the form, and choose Properties from the shortcut menu.

4. Click the Event tab of the Text Box: ContactName property sheet. The list of event procedures available for the text box control appears (see Figure 1-6).

Part I

ÉT Text Box; ContactName

ÉT Text Box; ContactName

Figure 1-6: Use the Build button, which is displayed as an ellipsis (... to create an event procedure. This button is not available unless an event is selected.

5. Click the Build button (...) to the right of the On Got Focus event procedure (Figure 1-6). This will bring up the Choose Builder dialog box, as shown in Figure 1-7.

Expression BiikJer Macro Bukfcr

[ « 1

| Cartel J

Figure 1-7: To write VBA programming code for your event procedure, choose Code Builder in the Choose Builder dialog box.

Figure 1-7: To write VBA programming code for your event procedure, choose Code Builder in the Choose Builder dialog box.

6. Select Code Builder in the Choose Builder dialog box and click OK. This will display a VBA code module in the Visual Basic Editor window (see Figure 1-8). This window (often referred to as VBE) is discussed in detail in Chapter 2.

Take a look at Figure 1-8. The name of the event procedure consists of three parts: the object name (ContactName), an underscore character (_), and the name of the event (GotFocus) occurring to that object. The word Private indicates that the event procedure cannot be triggered by an event from another form. The word Sub in the first line denotes the beginning of the event procedure. The words End Sub in the last line denote the end of the event procedure. The statements to be executed when the event occurs are written between these two lines. Notice that each procedure name ends with a pair of empty parentheses (). Words such as Sub, End, or Private have special meaning to Visual Basic and are called keywords (reserved words). Visual Basic displays keywords in blue, but you can change the color of your keywords from the Editor Format tab in the Options window (choose Tools | Options in the Visual Basic Editor window). All VBA keywords are automatically capitalized.

Introduction to Access 2003 VBA Programming

Introduction to Access 2003 VBA Programming

Figure 1-8: The Code Builder displays the event procedure Code window with a blank event procedure for the selected object. Here you can enter the code Access will run when the specified Got Focus procedure is triggered.

In the Code window (see Figure 1-8 above), there are two drop-down list boxes just below the title bar. The one on the left is called Object. This box displays the currently selected control. The box on the right is called Procedure. If you position the mouse over one of these boxes, the tool tip indicates the name of the box. By clicking on the down arrow to the right of the Procedure box, a list of all possible event procedures associated with the object type selected in the Object box will be displayed. You can close the drop-down list box by clicking anywhere in the unused portion of the Code window.

7. To change the background color of a text box control to green, enter the following statement between the existing lines:

Me.ContactName.BackColor = 65280

Notice that when you type each period, Visual Basic displays a list containing the possible item choices. This feature, called List Properties/Methods, is a part of Visual Basic's on-the-fly syntax and programming assistance, and is covered in Chapter 2. When finished, your first event procedure should look as follows:

Private Sub ContactName_GotFocus()

Me.ContactName.BackColor = 65280 End Sub

The statement you just entered tells Visual Basic to change the background color of the ContactName text box to green when the cursor is moved into that control. You can also specify the color by using the RGB function like this:

Me.ContactName.BackColor = RGB(0, 255, 0)

Part I

The above statement is equivalent to the statement you used earlier in the ContactName_GotFocus event procedure.

RGB Colors

Color values are combinations of red, green, and blue

components. The

RGB function has the following syntax:

RGB(red, green,

blue)

The intensity of red, green, and blue

can range from 0

to 255. Here are

some frequently used colors:

White

255,255, 255

Dark Green

0, 128, 0

Black

0, 0,0

Cyan

0, 255, 255

Gray

192,192, 192

Dark Cyan

0, 128, 128

Red

255, 0, 0

Blue

0, 0, 255

Dark Red

128, 0, 0

Dark Blue

0, 0, 128

Yellow

255, 255, 0

Magenta

255, 0, 255

Dark Yellow

128, 128, 0

Dark Magenta

128, 0, 128

Green

0, 255, 0

In the Visual Basic Code window, choose File | Close and Return to Microsoft Office Access. Notice that [Event Procedure] now appears next to the On Got Focus event property in the property sheet for the selected ContactName text box control (see Figure 1-9).

S? Text Box: ContactName

1 ContactName v

Format Data i Event | other All

Before Update

After qpdate

On Dirty

On Undo

On Change

On Enter

On Exit

OnLostFoos

|Event Procedure]

On dick

on Dbl Click

On House Down

Cn Mouse Move

Cn Mouse Up

On Key Down

OnKsyUp

On Key Press

1-1

Figure 1-9: [Event Procedure] in the property sheet denotes that the text box's Got Focus event has an event procedure associated with it.

Figure 1-9: [Event Procedure] in the property sheet denotes that the text box's Got Focus event has an event procedure associated with it.

9. To test your On Got Focus event procedure, activate the Customers form and choose View | Form View.

10. Click in the ContactName text box and notice the change in the background color.

11. Now, click on any other text box control.

Notice that the ContactName text box does not return to the original color. So far, you've only told Visual Basic what to do when the specified control receives the focus. If you want the background color to change when the focus moves to another control, there is one more event procedure to write — On Lost Focus. The code of this procedure is shown below. To create

Introduction to Access 2003 VBA Programming this procedure, return your form to Design view, click the ContactName control, and activate the properties sheet for this control. Click the Build button to the right of the On Lost Focus event property on the Event tab, select Code Builder, and proceed to write the statement that switches the background color to whatever it was before.

The completed On Lost Focus procedure looks like this:

Private Sub ContactName_LostFocus()

Me.ContactName.BackColor = 13434879 End Sub

12. Switch to the Customers form and run it by choosing View | Form View.

Test the events by clicking in and out of the ContactName text box.

Because objects recognize a sequence of events, it's important to understand what fires the events and the order in which they occur. In Part IV of this book, you'll learn what events can be used for a particular task to make your application smarter.

0 0

Post a comment