Microsoft KB Archive/131886

= ACC: Using Code to Set Control's Colors =

Article ID: 131886

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 2.0 Standard Edition
 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q131886





SUMMARY
Moderate: Requires basic macro, coding, and interoperability skills.

This article shows you how to create two user-defined Visual Basic for Applications Sub procedures that you can use for the Enter and Exit events of a control to set and reset the ForeColor and BackColor properties of that control.

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 background and foreground colors of the LastName control:

 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 OnEnter property of the LastName control to the following event procedure: Private Sub LastName_Enter Call SetControlColor([LastName]) End Sub NOTE: In version 2.0, there is a space in the Last Name field name. </li>  Set the OnExit property of the LastName control to the following event procedure: Private Sub LastName_Exit (Cancel As Integer) Call ReSetControlColor([LastName]) End Sub </li> Save and close the form.</li></ol>

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 foreground color changes to white.
 * 3) Press TAB to move to the next control. Note that the original colors return to the LastName control.

Additional query words: colour on entry exit original cursor cell how can I to do

Keywords: kbinfo kbprogramming kbusage KB131886

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.