Solved

Access 2013 Set Conditional format of multiple field in a form

Posted on 2014-02-09
9
748 Views
Last Modified: 2014-02-12
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
0
Comment
Question by:rogerdjr
  • 5
  • 3
9 Comments
 
LVL 14

Expert Comment

by:Bill Ross
ID: 39845510
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
0
 
LVL 16

Expert Comment

by:Sheils
ID: 39845853
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
0
 

Author Comment

by:rogerdjr
ID: 39849072
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
0
 
LVL 16

Accepted Solution

by:
Sheils earned 500 total points
ID: 39851338
You can use the onload event of the for the set the gotfocus and lostfocus events. Codes are as follows:

Private Sub Form_Load()

      For Each ctl In Me.Controls

      If ctl.ControlType = acTextBox Then

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


      End If

      Next

End Sub

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

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

End Function
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Closing Comment

by:rogerdjr
ID: 39851432
Works Perfectly simple and painless - thanks a million!!!
0
 
LVL 16

Expert Comment

by:Sheils
ID: 39851521
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.
0
 

Author Comment

by:rogerdjr
ID: 39851625
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
0
 
LVL 16

Expert Comment

by:Sheils
ID: 39852588
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
0
 
LVL 16

Expert Comment

by:Sheils
ID: 39852603
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
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS Access to SQL Conversion - Bit and Yes/No Datatypes 12 44
Access query with left expression 9 30
Query to summarise data Like Pivot Table 3 28
access 7 0
In case Office 2010 has not been deployed in your environment, this article may be quite useful. In our office, we wanted a way to deploy Microsoft Office Professional Plus 2010 through an automated batch file via logon script. This article is docum…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now