ACC: Change Active Control Background Color with Timer Event

Last reviewed: August 28, 1997
Article ID: Q141535
The information in this article applies to:
  • Microsoft Access versions 2.0, 7.0, 97

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 control's background color as long as the control 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 Customers form OnTimer property 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 Customers form's TimerInterval property 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.

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


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 28, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.