Online Book Reader

Home Category

Access Cookbook - Ken Getz [38]

By Root 2119 0
Const conFlat = 0

Create two functions named SpecialEffectEnter and SpecialEffectExit that will toggle the values of the BackColor and SpecialEffects properties for the text boxes. The completed module is shown in Figure 2-4.

Here are the code listings for the two functions:

Public Function SpecialEffectEnter( )

On Error GoTo HandleErr

' Set the current control to be indented.

Screen.ActiveControl.SpecialEffect = conIndent

' Set the current control's background color to be white.

Screen.ActiveControl.BackColor = conWhite

ExitHere:

Exit Function

HandleErr:

MsgBox Err & ": " & Err.Description

Resume ExitHere

End Function

Public Function SpecialEffectExit( )

On Error GoTo HandleErr

' Set the current control to be flat.

Screen.ActiveControl.SpecialEffect = conFlat

' Set the current control's background color to be gray.

Screen.ActiveControl.BackColor = conGray

ExitHere:

Exit Function

HandleErr:

MsgBox Err & ": " & Err.Description

Resume ExitHere

End Function

Figure 2-4. The completed basSpecialEffects module

Create your input form, if you haven't already. In design mode, select all of the text boxes to which you'd like to attach this effect. (Shift-clicking with the mouse allows you to select multiple controls.) When you select a group of controls, you can set properties for all of them at once. Set the properties of this group of controls as shown in Table 2-1. Figure 2-5 shows the design surface with all the text boxes selected. (Note that once you select multiple controls, the properties sheet's title can no longer display the name of the selected control and it will only show "Multiple selection," as shown in Figure 2-5.)

Figure 2-5. frmEffects in design mode, with all the text boxes selected

Table 2-1. Property settings for selected controls on frmEffects

Property

Value

BackColor

12632256

OnGotFocus

=SpecialEffectEnter( )

OnLostFocus

=SpecialEffectExit( )

Add the following code to the form's Load event procedure (see the Preface for information on creating event procedures):

Sub Form_Open (Cancel As Integer)

Me.SetFocus

End Sub

Discussion


The SpecialEffectEnter and SpecialEffectExit functions do their work by reacting to the events that occur when you enter or leave a control on the form. Every time you enter one of the text boxes to which you've attached a function, Access executes that function. Therefore, whenever you enter one of these special text boxes, Access will cause the text box to appear sunken and will change its background color to white. When you leave the control (by tab or mouseclick), Access will set it back to being flat and will reset its background color to gray.

The pair of functions do their work for any control by using the built-in Screen.ActiveControl object. This object always provides a reference to the currently active control. Therefore, when you enter a control, the function acts on that particular control, setting the SpecialEffects and BackColor properties.

The only problem with this mechanism is that, when Access first opens a form, there isn't a current control. Attempting to refer to Screen.ActiveControl before the form is fully loaded results in an Access error. Because Access attempts to enter the first control on your form when it first opens the form and there isn't yet a current control, the code you've attached to that first text box's OnGotFocus event property will fail. To work around this problem, you need to use the code attached to the Open event, as shown in Step 4. This tiny bit of code forces Access to load the form completely before it attempts to enter the first text box on the form. You may find this technique useful in other applications you create that use Screen.ActiveControl.

The functions used in this solution could be extended to include many other changes to the controls as you enter and leave them. For example, you can change the font or its size, or the foreground color. You might wonder why this example calls functions directly from the Properties window, instead

Return Main Page Previous Page Next Page

®Online Book Reader