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.
- Start Microsoft Access, and then open the sample database Northwind.mdb or the sample project NorthwindCS.adp.
- Open the Employees form in Design view.
- 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.
- On the Format menu, click Conditional Formatting.
- In the Condition 1 area of the Conditional Formatting dialog box, select Field Has Focus in the first box.
- 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. - 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.
- Start Microsoft Access, and then open the sample database Northwind.MDB or the sample project NorthwindCS.adp.
- 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
- Save the module as SetColorModule.
- Open the Employees form in Design view.
- Set the On Enter property of the LastName control to the following event procedure:
Private Sub LastName_Enter ()
Call SetControlColor([LastName])
End Sub
- 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
- Save and close the form.
To test the results, follow these steps:
- Open the Employees form in Form view.
- 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.
- 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.