The information in this article applies to:
- Microsoft Access versions 2.0, 7.0, 97
SUMMARY
Moderate: Requires basic macro, coding, and interoperability skills.
This article shows you how to create and use two user-defined Visual Basic
for Applications Sub procedures that you can use for a control's Enter and
Exit events to set and reset the control's ForeColor and BackColor
properties.
This article assumes that you are familiar with Visual Basic for
Applications and with creating Microsoft Access applications using the
programming tools provided with Microsoft Access. For more information
about Visual Basic for Applications, please refer to your version of the
"Building Applications with Microsoft Access" manual.
NOTE: Visual Basic for Applications is called Access Basic in Microsoft
Access version 2.0. For more information about Access Basic, please refer
to the "Building Applications" manual.
CAUTION: Following the steps in this example will modify the sample
database Northwind.mdb (or NWIND.MDB in version 2.0 or earlier). You may
want to back up the Northwind.mdb (or NWIND.MDB) file and perform these
steps on a copy of the database.
MORE INFORMATION
The following example demonstrates how to create and use the two Visual
Basic Sub procedures to set and reset the LastName control's background and
foreground colors:
- Open the sample database Northwind.MDB (or NWIND.MDB in version 2.0).
- Create a module and type the following lines in the Declarations
section:
Option Explicit
Dim SaveForeColor As Single
Dim SaveBackColor As Single
Const MyBackColor = 0
Const MyForeColor = 16777215
- Type the following two Sub procedures:
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 LastName control's OnEnter property to the following event
procedure:
Private Sub LastName_Enter ()
Call SetControlColor([LastName])
End Sub
NOTE: In version 2.0, there is a space in Last Name.
- Set the LastName control's OnExit property 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
control's background color changes to black and the control's
foreground color changes to white.
- Press TAB to move to the next control. Note that the original colors
return to the LastName control.
Keywords : kbusage PgmHowTo FmsHowTo FmsProp
Version : 2.0 7.0 97
Platform : WINDOWS
Hardware : x86
Issue type : kbinfo