ACC: How to Use Code to Set the Colors of a Control

ID: Q131886

The information in this article applies to:

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:

1. Open the sample database Northwind.MDB (or NWIND.MDB in version 2.0).

2. 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

3. 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

4. Save the module as SetColorModule.

5. Open the Employees form in Design view.

6. 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.

7. 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

8. 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 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:
Keywords          : kbusage FmsProp FmsHowto 
Version           : 2.0 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbinfo

Last Reviewed: November 21, 1998