ACC2000: How to Change the Color of the Control That Has Focus on a Form (210263)



The information in this article applies to:

  • Microsoft Access 2000

This article was previously published under Q210263
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

SUMMARY

You can set the formatting of a control on a form (for example, a text box) to display a different font color and background color when that control has focus. This can be accomplished by assigning either conditional formatting to the control or Visual Basic for Applications procedures to the On Enter and On Exit events of the control.

MORE INFORMATION

To facilitate data entry, you can format the controls on a form so that their font, background colors, and other formatting features change whenever they have focus.

Method 1: Using Conditional Formatting

To change the font and background colors of controls on a form, follow these steps:

CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.

  1. Start Microsoft Access, and then open the sample database Northwind.mdb or the sample project NorthwindCS.adp.
  2. Open the Employees form in Design view.
  3. Click the FirstName text box, and then, while pressing the SHIFT key, click the LastName, Title, ReportsTo, HireDate, and Extension text boxes to select all of them.
  4. On the Format menu, click Conditional Formatting.
  5. In the Condition 1 area of the Conditional Formatting dialog box, select Field Has Focus in the first box.
  6. Select the font style, color, and/or any other formatting that you want the field to have when it has focus. For this example, click the Font Color button's arrow, and then select the color red. Click the Background Color button's arrow, select the color yellow, and then click OK.

    NOTE: When you open the Conditional Formatting dialog box, it displays the current default formatting for the selected control, including colors, font styles, and any expressions that you may have defined for the field.
  7. On the View menu, click Form View.
Notice that each text box with focus displays different colors (red and yellow, in this case), and returns to the default colors when you press the TAB key to change the focus to the next control.

Method 2: Using Visual Basic for Applications Code

You can also use the following two Visual Basic Sub procedures to set and reset the background and foreground colors of the LastName control:

CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.

  1. Start Microsoft Access, and then open the sample database Northwind.MDB or the sample project NorthwindCS.adp.
  2. Create a new module, and then paste or type the following code:
    Option Explicit
    Dim SaveForeColor As Single
    Dim SaveBackColor As Single
    
    Const MyBackColor = 0
    Const MyForeColor = 16777215
    
    Sub SetControlColor (MyControl As Control)
    ' ******************************************************************
    ' Sub:     SetControlColor
    '
    ' Purpose: This procedure sets the colors of the referenced control.
    ' ******************************************************************
    
    On Local Error Resume Next
    
    ' Save the current control colors.
    SaveBackColor = MyControl.BackColor
    SaveForeColor = MyControl.ForeColor
    
    ' Set the custom colors.
    MyControl.BackColor = MyBackColor
    MyControl.ForeColor = MyForeColor
    
    End Sub
    
    Sub ReSetControlColor (MyControl As Control)
    ' ***********************************************************
    ' Sub: ReSetControlColor
    '
    ' Purpose: This procedure resets the colors of the referenced
    '          control.
    ' ***********************************************************
    
    On Local Error Resume Next
    
    ' Reset to the saved colors.
    MyControl.BackColor = SaveBackColor
    MyControl.ForeColor = SaveForeColor
    
    End Sub
    					
  3. Save the module as SetColorModule.
  4. Open the Employees form in Design view.
  5. Set the On Enter property of the LastName control to the following event procedure:
    Private Sub LastName_Enter ()
      Call SetControlColor([LastName])
    End Sub
  6. Set the On Exit property of the LastName control to the following event procedure:
    Private Sub LastName_Exit (Cancel As Integer)
      Call ReSetControlColor([LastName])
    End Sub
    					
  7. Save and close the form.
To test the results, follow these steps:
  1. Open the Employees form in Form view.
  2. Press the TAB key to move to the LastName control. Note that the background color of the control changes to black and the foreground color changes to white.
  3. Press TAB to move to the next control. Note that the original colors return to the LastName control.NOTE: Conditional formatting takes precedence over the Visual Basic procedures. If conditional formatting has been set for a control, the SetControlColor and ReSetControlColor procedures have no effect.

REFERENCES

For more information about applying conditional formatting to controls, click Microsoft Access Help on the Help menu, type conditional formatting in a text box or other control on a form or report in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Modification Type:MajorLast Reviewed:6/24/2004
Keywords:kbhowto kbinfo kbusage KB210263