Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change all fields backcolor on Got Focus

Status
Not open for further replies.

Debbie37

Technical User
Feb 23, 2002
28
US
I know you can change the backcolor of a field on the Got Focus event, but is there a way to change the backcolor when any field on a form (or database) has the focus that does not include code on the Got Focus (13434879-yellow) AND Lost Focus (16777215-back to white) events?

.....thanks!
 
Debbie37

Have you tried Format - Conditional Formatting from the Toolbar?



JR
 
Just wondering how do you set the backcolour of a text box on the Got Focus event pls could you post the code if possible.

Regards

Andy
 
Andy,

on GOTFOCUS:
CONTROLX.BACKCOLOR=13434879

ON LOSTFOCUS
CONTROLX.BACKCOLOR=16777215
REGARDS, KUZZ

LIFE IS GREAT WHEN THERE IS SOMEONE TO HELP YOU.
DESIGNING DATABASES SINCE 2002
 
Debbie,
Conditional formatting is the only solution
see Formbar, Format menu REGARDS, KUZZ

LIFE IS GREAT WHEN THERE IS SOMEONE TO HELP YOU.
DESIGNING DATABASES SINCE 2002
 
Here is what I would do.

Code:
Private Sub txtProductID_GotFocus()
    f_focusColorChange "got"
End Sub

Private Sub txtProductID_LostFocus()
    f_focusColorChange "lost"
End Sub


Public Function f_focusColorChange(szFocustype As String)
    Dim ctl As Control
    Dim frm As Form
    Set frm = Me
    ' Enumerate Controls collection.
    For Each ctl In frm.Controls
        ' Check to see if control is text box.
        If ctl.ControlType = acTextBox Then
            ' Set control properties.
            With ctl
                If szFocustype = "got" Then
                    Debug.Print .BackColor
                    .BackColor = 13434879
                Else
                    .BackColor = 16777215
                End If
            End With
        End If
    Next ctl
End Function
 
that is one hell of a solution. not the fastest, but the most efficient in the long run REGARDS, KUZZ

LIFE IS GREAT WHEN THERE IS SOMEONE TO HELP YOU.
DESIGNING DATABASES SINCE 2002
 
Here is another way. Pretty much the same as the first but less memory is used for instantiated objects.

Code:
Public Function f_focusColorChange2(szFocustype As String)
    Dim i As Integer
    Dim iCTLCount As Integer
    iCTLCount = Me.Controls.Count
    For i = 0 To iCTLCount - 1
        If Me.Controls.Item(i).ControlType = acTextBox Then
            If szFocustype = "got" Then
                Me.Controls.Item(i).BackColor = 13434879
            Else
                Me.Controls.Item(i).BackColor = 16777215
            End If
        End If
    Next
   
End Function
 
OK i have done the above and like i say that works well for the form but now i would like to do it individually ie when a text box/ combo box on got focus i can put in
i.e
Private Sub Postcode_GotFocus()
Postcode.BackColor = 13434879
End Sub

Private Sub Postcode_LostFocus()
Postcode.BackColor = 16777215
End Sub

which is fine but i would have to put that behind everything how would i go about writing a global to call by saying something along the lines

Private Sub Postcode_GotFocus()
Yellow
End Sub

Private Sub Postcode_LostFocus()
White
End Sub
 
Hi!

Create the following public function in a module:

Public Function ChangeBackColor()

Dim strCurrentControl As String
Dim strPreviousControl As String
Dim strFormName As String

strCurrentControl = Screen.ActiveControl.Name
strPreviousControl = Screen.PreviousControl.Name
strFormName = Screen.ActiveForm.Name

Forms(strFormName).Controls(strCurrentControl).BackColor = Yellow
Forms(strFormName).Controls(strPreviousControl).BackColor = White

End Function

Then in the property window for each control go to the Events tab and in the On Got Focus type =ChangeBackColor()

hth
Jeff Bridgham
bridgham@purdue.edu
 
Cant get that code to work
it highlights the following
strCurrentControl = Screen.ActiveControl.Name

any thing else i can do

Thanks

Andy
 
Hi Andy!

Try it in stages:

Dim ctlCurrent As Control
Dim ctlPrevious As Control

Set ctlCurrent = Screen.ActiveControl
Set ctlPrevious = Screen.PreviousControl

ctlCurrent.BackColor = Yellow
ctlPrevious.BackColor = White

hth
Jeff Bridgham
bridgham@purdue.edu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top