Link to home
Start Free TrialLog in
Avatar of rogerdjr
rogerdjrFlag for United States of America

asked on

Access 2013 Set Conditional format of multiple field in a form

Is there an easy way to set-up a form so that each field changes color when it is the field that has current focus for input?

I tried selecting multiple fields and right clicking and the conditional format is not on the menu

Maybe some vba code that runs when a form open and goes through all fields to set the conditional format so that the back color of the field is light blue when it has focus?

Thanks
Avatar of Bill Ross
Bill Ross
Flag of United States of America image

Hi,

Not sure about 2013 but I used to get this effect in other versions like this:

Create a label.  
Set the background of the label to the desired "in focus" color.
Use the ToolBar to change the label to a text box.

I think there is a property for the text box that can be set but this might get you started.

Regards,

Bill
Use the gotfocus and lost focus event of the control that you want to change color. Example if you want a textbox to change color:

Select the textbox in design view and go to it's property
On the event tab find On Got Focus and double click on it
The VB window will open insert the following code:

Private Sub TextBoxName_GotFocus()

Me.TextBoxName.BackColor = vbGreen

End Sub

Back to the form and find the On Lost Focus property and set it event to

Private Sub TextBoxName_LostFocus()

Me.TextBoxName.BackColor = vbWhite

End Sub
Avatar of rogerdjr

ASKER

It is easier to go text box by text box and set the conditional format. With 20 +/- items per form this can be a pain I was hoping for a "global" process that accomplishes the same thing
ASKER CERTIFIED SOLUTION
Avatar of Sheils
Sheils
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works Perfectly simple and painless - thanks a million!!!
Glad you're happy.

You can further reduce the code by moving the changecolor function to a module so that you don't have to add it to each form. But you will have to add reference to the form name for it to work because you can use Me. in module.
Something like?

Private Sub Form_Load()

 For Each ctl In Me.Controls

      If ctl.ControlType = acTextBox Then

            ctl.OnGotFocus = "=ChangeColor('" & me.form.name & "', '" & ctl.Name & "'," & " True)"
            ctl.OnLostFocus = "=ChangeColor('" & me.form.name & "', '" & ctl.Name & "'," & " false)"
      End If
      Next
End Sub


with this in a module?


Function ChangeColor(frmName As String, ctlName As String, booFocus As Boolean)

    If booFocus = True Then
   
        frmName(ctlName).BackColor = vbGreen
       
    Else
   
        frmName(ctlName).BackColor = vbWhite
       
    End If

End Function
try

Private Sub Form_Load()

For Each ctl In Me.Controls

      If ctl.ControlType = acTextBox Then

            ctl.OnGotFocus = "=ChangeColor('" & me.name & "', '" & ctl.Name & "'," & " True)"
            ctl.OnLostFocus = "=ChangeColor('" & me.name & "', '" & ctl.Name & "'," & " false)"

      End If
 Next

 End Sub
 
with this in a module

Function ChangeColor(frmName As String, ctlname As String, booFocus As Boolean)

    If booFocus = True Then
   
       Forms(frmName).Controls(ctlname).BackColor = vbGreen
             
    Else
   
        Forms(frmName).Controls(ctlname).BackColor = vbWhite
       
    End If
End Function
NB:- The function will not work with textbox that are on subforms. It needs to be modified to work with subform. If you need more submit your question as a related question