ACC2000: How to Change Active Control Background Color with Timer Event (210187)



The information in this article applies to:

  • Microsoft Access 2000

This article was previously published under Q210187
Advanced: Requires expert coding, interoperability, and multiuser skills.

SUMMARY

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.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

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. You may want to back up the Northwind.mdb or perform these steps on a copy of the Northwind database.
  1. Open the sample database Northwind.mdb.
  2. In the Database window, click Modules under Objects, 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 ()      
    
    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.
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 effect that you want.

REFERENCES

For more information about the background colors, click Microsoft Access Help on the Help menu, type Change the background color of a control or section in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbhowto kbProgramming kbusage KB210187