HandsOn Responding to Control Events in a Class

This hands-on requires prior completion of Custom Project 27-1.

1. Activate the Visual Basic Editor window and choose Insert | Class Module. A new class named Class1 will appear in the Project Explorer window.

2. In the Properties window, click the (Name) property and type UCaseBox as the new name of Class1. Click again on the (Name) property to save the new name. You should see the UCaseBox entry under the Class Modules folder in the Project Explorer.

3. In the UCaseBox class module's Code window, enter the following code: Private WithEvents txtBox As Access.TextBox

Public Function InitializeMe(myTxt As TextBox) Set txtBox = myTxt txtBox.OnKeyPress = "[Event Procedure]" End Function

Private Sub txtBox_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case 48 To 57 MsgBox "Numbers are not allowed!" KeyAscii = 0 Case Else

' convert to uppercase KeyAscii = Asc(UCase(Chr(KeyAscii))) End Select txtBox.FontBold = True txtBox.FontItalic = True txtBox.BackColor = vbYellow End Sub

Notice that to respond to a control's events in a class module you start by declaring a module-level object variable using the WithEvents keyword. In our text box example, we declared the object variable txtBox as an Access text box control.

Because the form can contain more than one text box control, we should tell the class which text box it needs to respond to. We do this by creating a Property Set procedure (similar to the one created in Custom Project 27-1) or a function procedure like the one shown above. We called this function InitializeMe, but you can use any name you wish. Recall from Chapter 8 that a function entered in a class module serves as an object's method. We will call the InitializeMe method later from a form class module and pass it the actual control we want it to respond to (see step 8). The

Event Programming in Forms and Reports

InitializeMe method will assign the passed control to the WithEvents object variable like this:

Set txtBox = myTxt

Next, we set the text box KeyPress property to "[Event Procedure]" to tell the class that we are interested in tracking this particular event.

Finally, we write the event procedure code for the text box control's KeyPress event. This code begins by checking the value of the key that was pressed by the user. If a number was entered, the user is advised that numbers aren't allowed and the digit is removed from the text box by setting the value of KeyAscii to zero (0). Otherwise, if the user typed a lowercase letter, the character is converted to uppercase using the following statement:

KeyAscii = Asc(UCase(Chr(KeyAscii)))

KeyAscii is an integer that returns a numerical ANSI keycode. To convert the KeyAscii argument into a character, we use the Chr function:


Once we've converted a key into a character we use the UCase function to convert it to uppercase:


Finally, we translate the character back to an ANSI number by using the Asc function:


The txtBox_KeyPress event procedure ends by adding some visual enhancements to the text box. The text entered in it will appear in italics and bold type on a yellow background.

4. Save the code you entered in the UCaseBox class module's Code window by pressing the Save button on the toolbar.

5. In Design view, open the frmProducts form you created in Custom Project 27-1 earlier in this chapter.

6. Choose View | Code to switch to the frmProducts form's Code window.

The Form_frmProducts class module's Code window should already contain code you entered while working with Part 4 of Custom Project 27-1 earlier in this chapter. To connect the UCaseBox class module with the actual text box on any Access form, you would need to enter the code as shown below in a form's class module (do not enter it yet):

' module-level variable declaration Private clsTextBox! As UCaseBox

Part IV

Private Sub Form_Open(Cancel As Integer) Set clsTextBox1 = New UCaseBox clsTextBox1.InitializeMe Me.Controls("ProductName") End Sub

Private Sub Form_Close()

Set clsTextBox1 = Nothing End Sub

Since the frmProducts form already contains a call to the cRecordLogger class created earlier, all of the procedures we need are already in place; therefore, we will simply add the appropriate lines of code to the existing procedures. So let's proceed.

7. In the Form_frmProducts Code window, enter the following module-level variable declaration just above the Form_Open event procedure (see Figure 27-9):

Private clsTextBox1 As UCaseBox

The above statement declares the clsTextBox1 class variable. This variable is used in instantiating the UCaseBox object and connecting it with the actual text box control on the form (see step 8 below).

8. Enter the following lines of code before the End Sub of the Form_Open event procedure (see Figure 27-9):

Set clsTextBox1 = New UCaseBox clsTextBox1.InitializeMe Me.Controls("ProductName")

Before our UCaseBox class can respond to a text box's events you need these two lines of code; the first one sets the class variable clsTxtBox1 to a new instance of the UCaseBox class, and the second one calls the class InitializeMe method and supplies it with the name of the text box control.

9. Enter the following line of code before the End Sub of the Form_Close event procedure (see Figure 27-9):

Set clsTextBox1 = Nothing

When we are done with the object variable, we set it to Nothing to release the resources that have been assigned to it.

Event Programming in Forms and Reports

H Acc2003_Chap27 - Forri>_fmiPmduels (Code)

Event Programming in Forms and Reports

H Acc2003_Chap27 - Forri>_fmiPmduels (Code)

Figure 27-9: The form class module shows code that instantiates and hooks up objects created in the cRecordLogger and UCaseBox class modules with the form and text box control.

10. Save the changes made in the Code window by clicking the Save button on the toolbar.

11. Open the frmProducts form in Form view and choose Records | Data Entry.

12. Enter prune butter in the Product Name text box. Notice that as you type, the characters you enter are converted to uppercase. They are also made bold and italics, and appear on a yellow background. If you happen to press a number key that is disallowed by your custom KeyPress event, you receive an error message.

13. Close the frmProducts form. Because this form also responds to the AfterUpdate event that we programmed in Custom Project 27-1, you should see two message boxes as you exit this form.

0 0

Post a comment