ACC: Change Active Control Background Color with Timer Event

ID: Q141535

The information in this article applies to:

SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

This articles demonstrates a method that you can use to change the background color of the active control on a form. This method will change the background color of any active control that supports the BackColor property.

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.

MORE INFORMATION

To create the method that changes the background color of the active control on a form, follow these steps.

CAUTION: Following the steps in this example will modify the sample database Northwind.mdb (or NWIND.MDB in version 2.0). You may want to back up the Northwind.mdb (or NWIND.MDB) or perform these steps on a copy of the Northwind database.

1. Open the sample database Northwind.mdb.

2. Click the Module tab, and then click New.

3. In the Declarations section of the module type the following line:

      Global myctrname as String

4. Save the module as Module1, and then close the module.

5. Open the Customers form in Design view.

6. Set the OnTimer property of the Customers form to the following event

   procedure:

      Private Sub form_Timer ()         ' In Microsoft Access 2.0, the word
                                        ' "Private" will not appear in this
                                        ' line of the code.

      Dim lngYellow As Long, lngWhite As Long
          On Error GoTo Errhandler
          lngYellow = RGB(255, 255, 0)     'Set lngYellow variable for
                                           'yellow color.
          lngWhite = RGB(255, 255, 255)    'Set lngWhite variable for white
                                           'color.

          If Screen.ActiveControl.Name <> myctrname Then  ' If active
                                                          ' control not
                                                          ' equal to
                                                          ' myctrname do
                                                          ' next line.
          Me(myctrname).BackColor = lngWhite            ' Set myctrname
                                                        ' variable to white
                                                        ' BackColor.
          Screen.ActiveControl.BackColor = lngYellow    ' Set active color
                                                        ' BackColor to
                                                        ' yellow.
          myctrname = Screen.ActiveControl.Name         ' Set myctrname
                                                        ' variable to
                                                        ' active control
                                                        ' name.
          End If

      Exit Sub

      Errhandler:
      If Err = 2465 Then        ' If error is 2465 which is "Object-defined
                                ' error."
      Resume Next               ' Resume running on next line after error.
      ElseIf Err = 2474 Then    ' If error is 2474 which is "No Control is
                                ' active."
      Resume Next
      Else
         MsgBox Err & " " & Error   ' Show Error number and string of error
                                    ' value.
      Exit Sub
      End If

      End Sub

7. Set the TimerInterval property of the Customers form to 200.

8. View the Customers form in Form view. Press TAB to move from one control

   to another. Note that the background color changes from white to yellow
   for each control that receives the focus.

REFERENCES

For more information about this topic, search for "backcolor," and then "Change the background color of a control or section" using the Microsoft Access 97 Help Index.

NOTE: The TimerInterval property uses milliseconds (1000 = One second). Smaller numbers produce quicker responses. You may have to experiment with the numbers to achieve the desired effect.

Additional query words:

Keywords          : kbusage PgmObj 
Version           : 2.0 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto

Last Reviewed: November 21, 1998