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.
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.


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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

757 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

20 Experts available now in Live!

Get 1:1 Help Now