Change Form background based on field value

Posted on 2014-01-16
Last Modified: 2014-01-16
I would like the form background to be based on  a value in a field. The field name is "office". I am guessing it would be an "After Update" event?

If Office is the following values, I would like the background color to change to the given color:

Value in Office  Desired Color
OC                         Light Green
RS                          Light Blue
SD                         Blue
LA                          Blue-ish Purple
SV                          Yellow
680                        Light Red
Question by:cansevin
  • 3
  • 3
LVL 84
ID: 39786504
You'd need to use the Form's Current event:

Sub Form_Current()
  Select Case Me!Office
    Case "OC"
      Me.Detail.Backcolor = vbGreen
    Case "RS"
      Me.Detail.Backcolor = vbBlue
     etc etc
  End Select
End Sub

Note that "vbGreen" is a constant. You can also define a specific color, if you know the correct values. For example "lightseagreen" is 11186720, so you could do this:

Me.Detail.Backcolor = 11186720

Here's a list of colors:

If you're using Form Headers and Footers you'll need to set the BackColor of those as well:

Me.FormHeader.BackColor = 11186720

Author Comment

ID: 39786524
Thanks... when would it change colors... immediately after we update it or when we click onto the form?
LVL 84
ID: 39786723
The Current event fires for each record, so it would fire whenever you changed to a different record.

If you want this to fire only when the form is opened, then move it to the Form Open or Load event.

If you want it to fire when you update the value in a Control, then put it in that control's After Update event.
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.


Author Comment

ID: 39786790
It is almost working perfectly... the only issue is when there isn't a value on the next form, it stay the color of the prior form. Do I have to put a "case" for when it is null? If so, how? This is the current code I am using:

Private Sub Form_Current()

Select Case Me!cmOffice
     Case "Orange County"
      Me.Detail.BackColor = vbGreen
    Case "Riverside"
     Me.Detail.BackColor = vbBlue
End Select

End Sub
LVL 84

Accepted Solution

Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 39787168
Yes ... use a Case Else for those sorts of things (and you may want to use the Nz function also):

Select Case Nz(Me!cmOffice, "")
     Case "Orange County"
      Me.Detail.BackColor = vbGreen
    Case "Riverside"
     Me.Detail.BackColor = vbBlue
    Case Else
      Me.Detail.BackColor = vbWhite
End Select

Author Closing Comment

ID: 39787226
Thanks... works great. I am going to open another question for an "after update" code. If you can help on that, that would be awesome. Thanks again!

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

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 Query Question 2 36
Find unused columns in a table 12 75
What can I do to shrink my Access Frontend? 12 45
Sort order not as I expected 13 21
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

726 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