in report,field background color based on field value

Posted on 2004-11-09
Last Modified: 2007-11-27
I'm using MS Access
I have a report based on a query
In the report I need to assign a background color based on the value of one of the fields in the recordset.

this is what I'm trying and want to know
Is this the most efficient way?
it does not work well, what am I missing?

Your help is welcomed and appreciated.
Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
Const yellow = 10416380
Const blue = 15328965
Const lavender = 14202080
Const pink = 15654399
Const peach = 8573437
Const green = 12320699
Const purple = 16752591
Dim rptConcreteTransactions As Report

If rptConcreteTransactions!dayID = 1 Then
    rptConcreteTransactions![scheduleDate].BackColor = blue
    rptConcreteTransactions![dayName].BackColor = blue
    rptConcreteTransactions![ScheduleTime].BackColor = blue
    rptConcreteTransactions![dayName].BackColor = blue
    rptConcreteTransactions![MIXNUM].BackColor = blue
    rptConcreteTransactions![VENDOR].BackColor = blue
    rptConcreteTransactions![WILLCALL].BackColor = blue
    rptConcreteTransactions![COSTCODE].BackColor = blue
    rptConcreteTransactions![SEGMENT].BackColor = blue
    rptConcreteTransactions![WORK_ITEM].BackColor = blue
    rptConcreteTransactions![DESCRIPTION].BackColor = blue
    rptConcreteTransactions![SUBELEMENT].BackColor = blue
    rptConcreteTransactions![ORDERNUM].BackColor = blue
    rptConcreteTransactions![QTYORDERED].BackColor = blue
    rptConcreteTransactions![QTYRECVD].BackColor = blue
    rptConcreteTransactions![ACTQTYUSED].BackColor = blue
    rptConcreteTransactions![LNAME].BackColor = blue
    rptConcreteTransactions![DIRECTIONS].BackColor = blue
    rptConcreteTransactions![DELIVERYINTERVAL].BackColor = blue
 If rptConcreteTransactions.dayID = 2 Then
    rptConcreteTransactions![scheduleDate].BackColor = pink
    rptConcreteTransactions![dayName].BackColor = pink
    rptConcreteTransactions![ScheduleTime].BackColor = pink
    rptConcreteTransactions![dayName].BackColor = pink
    rptConcreteTransactions![MIXNUM].BackColor = pink
    rptConcreteTransactions![VENDOR].BackColor = pink
    rptConcreteTransactions![WILLCALL].BackColor = pink
    rptConcreteTransactions![COSTCODE].BackColor = pink
    rptConcreteTransactions![SEGMENT].BackColor = pink
    rptConcreteTransactions![WORK_ITEM].BackColor = pink
    rptConcreteTransactions![DESCRIPTION].BackColor = pink
    rptConcreteTransactions![SUBELEMENT].BackColor = pink
    rptConcreteTransactions![ORDERNUM].BackColor = pink
    rptConcreteTransactions![QTYORDERED].BackColor = pink
    rptConcreteTransactions![QTYRECVD].BackColor = pink
    rptConcreteTransactions![ACTQTYUSED].BackColor = pink
    rptConcreteTransactions![LNAME].BackColor = pink
    rptConcreteTransactions![DIRECTIONS].BackColor = pink
    rptConcreteTransactions![DELIVERYINTERVAL].BackColor = pink
 If rptConcreteTransactions.dayID = 3 Then
    rptConcreteTransactions![scheduleDate].BackColor = lavender
    rptConcreteTransactions![dayName].BackColor = lavender
    rptConcreteTransactions![ScheduleTime].BackColor = lavender
    rptConcreteTransactions![dayName].BackColor = lavender
    rptConcreteTransactions![MIXNUM].BackColor = lavender
    rptConcreteTransactions![VENDOR].BackColor = lavender
    rptConcreteTransactions![WILLCALL].BackColor = lavender
    rptConcreteTransactions![COSTCODE].BackColor = lavender
    rptConcreteTransactions![SEGMENT].BackColor = lavender
    rptConcreteTransactions![WORK_ITEM].BackColor = lavender
    rptConcreteTransactions![DESCRIPTION].BackColor = lavender
    rptConcreteTransactions![SUBELEMENT].BackColor = lavender
    rptConcreteTransactions![ORDERNUM].BackColor = lavender
    rptConcreteTransactions![QTYORDERED].BackColor = lavender
    rptConcreteTransactions![QTYRECVD].BackColor = lavender
    rptConcreteTransactions![ACTQTYUSED].BackColor = lavender
    rptConcreteTransactions![LNAME].BackColor = lavender
    rptConcreteTransactions![DIRECTIONS].BackColor = lavender
    rptConcreteTransactions![DELIVERYINTERVAL].BackColor = lavender
 If rptConcreteTransactions.dayID = 4 Then
    rptConcreteTransactions![scheduleDate].BackColor = peach
    rptConcreteTransactions![dayName].BackColor = peach
    rptConcreteTransactions![ScheduleTime].BackColor = peach
    rptConcreteTransactions![dayName].BackColor = peach
    rptConcreteTransactions![MIXNUM].BackColor = peach
    rptConcreteTransactions![VENDOR].BackColor = peach
    rptConcreteTransactions![WILLCALL].BackColor = peach
    rptConcreteTransactions![COSTCODE].BackColor = peach
    rptConcreteTransactions![SEGMENT].BackColor = peach
    rptConcreteTransactions![WORK_ITEM].BackColor = peach
    rptConcreteTransactions![DESCRIPTION].BackColor = peach
    rptConcreteTransactions![SUBELEMENT].BackColor = peach
    rptConcreteTransactions![ORDERNUM].BackColor = peach
    rptConcreteTransactions![QTYORDERED].BackColor = peach
    rptConcreteTransactions![QTYRECVD].BackColor = peach
    rptConcreteTransactions![ACTQTYUSED].BackColor = peach
    rptConcreteTransactions![LNAME].BackColor = peach
    rptConcreteTransactions![DIRECTIONS].BackColor = peach
    rptConcreteTransactions![DELIVERYINTERVAL].BackColor = peach
 If rptConcreteTransactions.dayID = 5 Then
    rptConcreteTransactions![scheduleDate].BackColor = green
    rptConcreteTransactions![dayName].BackColor = green
    rptConcreteTransactions![ScheduleTime].BackColor = green
    rptConcreteTransactions![dayName].BackColor = green
    rptConcreteTransactions![MIXNUM].BackColor = green
    rptConcreteTransactions![VENDOR].BackColor = green
    rptConcreteTransactions![WILLCALL].BackColor = green
    rptConcreteTransactions![COSTCODE].BackColor = green
    rptConcreteTransactions![SEGMENT].BackColor = green
    rptConcreteTransactions![WORK_ITEM].BackColor = green
    rptConcreteTransactions![DESCRIPTION].BackColor = green
    rptConcreteTransactions![SUBELEMENT].BackColor = green
    rptConcreteTransactions![ORDERNUM].BackColor = green
    rptConcreteTransactions![QTYORDERED].BackColor = green
    rptConcreteTransactions![QTYRECVD].BackColor = green
    rptConcreteTransactions![ACTQTYUSED].BackColor = green
    rptConcreteTransactions![LNAME].BackColor = green
    rptConcreteTransactions![DIRECTIONS].BackColor = green
    rptConcreteTransactions![DELIVERYINTERVAL].BackColor = green
If rptConcreteTransactions.dayID = 6 Then
    rptConcreteTransactions![scheduleDate].BackColor = yellow
    rptConcreteTransactions![dayName].BackColor = yellow
    rptConcreteTransactions![ScheduleTime].BackColor = yellow
    rptConcreteTransactions![dayName].BackColor = yellow
    rptConcreteTransactions![MIXNUM].BackColor = yellow
    rptConcreteTransactions![VENDOR].BackColor = yellow
    rptConcreteTransactions![WILLCALL].BackColor = yellow
    rptConcreteTransactions![COSTCODE].BackColor = yellow
    rptConcreteTransactions![SEGMENT].BackColor = yellow
    rptConcreteTransactions![WORK_ITEM].BackColor = yellow
    rptConcreteTransactions![DESCRIPTION].BackColor = yellow
    rptConcreteTransactions![SUBELEMENT].BackColor = yellow
    rptConcreteTransactions![ORDERNUM].BackColor = yellow
    rptConcreteTransactions![QTYORDERED].BackColor = yellow
    rptConcreteTransactions![QTYRECVD].BackColor = yellow
    rptConcreteTransactions![ACTQTYUSED].BackColor = yellow
    rptConcreteTransactions![LNAME].BackColor = yellow
    rptConcreteTransactions![DIRECTIONS].BackColor = yellow
    rptConcreteTransactions![DELIVERYINTERVAL].BackColor = yellow
If rptConcreteTransactions.dayID = 7 Then
    rptConcreteTransactions![scheduleDate].BackColor = purple
    rptConcreteTransactions![dayName].BackColor = purple
    rptConcreteTransactions![ScheduleTime].BackColor = purple
    rptConcreteTransactions![dayName].BackColor = purple
    rptConcreteTransactions![MIXNUM].BackColor = purple
    rptConcreteTransactions![VENDOR].BackColor = purple
    rptConcreteTransactions![WILLCALL].BackColor = purple
    rptConcreteTransactions![COSTCODE].BackColor = purple
    rptConcreteTransactions![SEGMENT].BackColor = purple
    rptConcreteTransactions![WORK_ITEM].BackColor = purple
    rptConcreteTransactions![DESCRIPTION].BackColor = purple
    rptConcreteTransactions![SUBELEMENT].BackColor = purple
    rptConcreteTransactions![ORDERNUM].BackColor = purple
    rptConcreteTransactions![QTYORDERED].BackColor = purple
    rptConcreteTransactions![QTYRECVD].BackColor = purple
    rptConcreteTransactions![ACTQTYUSED].BackColor = purple
    rptConcreteTransactions![LNAME].BackColor = purple
    rptConcreteTransactions![DIRECTIONS].BackColor = purple
    rptConcreteTransactions![DELIVERYINTERVAL].BackColor = purple
End If

End Sub
Question by:lallentx
    LVL 26

    Accepted Solution

    Hi lallentx

    Set your constants outside the sub, so they only get set once at module level.
    If the report object you are instantiating is the current report, there is no need to declare a report object, just use Me.
    If you do need to declare a report object, instantiate it outside the sub, so it only gets set once at module level.

    Option Explicit

    Const yellow = 10416380
    Const blue = 15328965
    Const lavender = 14202080
    Const pink = 15654399
    Const peach = 8573437
    Const green = 12320699
    Const purple = 16752591

    Dim mlngColour As Long

    Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
      Select Case dayID
        Case 1
          mlngColour = blue
        Case 2
          mlngColour = pink
        Case 3
          mlngColour = lavender
        Case 4
          mlngColour = peach
        Case 5
          mlngColour = green
        Case 6
          mlngColour = yellow
        Case 7
          mlngColour = purple
        Case Else
          mlngColour = 16777215  ' White
      End Select
      Dim I As Integer
      For I = 0 To Me.Controls.Count - 1
        If TypeOf Me.Controls(I) Is TextBox Then
          Me.Controls(I).Properties("BackColor") = mlngColour
        End If
      Next I

    End Sub



    Author Comment


    it is obvious to me that my approach was poorly written. I see what you are doing and understand how you set the case structure however, there is one thing I don't follow.

    how do I connect the dayID value to the colors? in other words, how do I tell the program to:
    if dayid = 1 then blue?

    how is this incorporated?

    i can see that the case structure is called dayID but is that all is needed to incorporate the dayID value?

    I removed my code in placed yours instead but I do not see any changes in the fields background.

    thanks so much for your help
    LVL 26

    Expert Comment

    by:Alan Warren
    Hi lallentx

    Did you set the BackStyle property to normal, think it is  Transparent by default.
    you can set it in VBA

      Me.Controls(I).Properties("BackStyle") = 1

    LVL 26

    Expert Comment

    by:Alan Warren
    Re: is that all is needed to incorporate the dayID value?

    yes so long as your variable or control called DayID has a value, the  select case will only execute the code for the  correct case, so if dayID  is 1 then mlngColour = blue

    Its woking here ok, I didnt have a field or variable called DayID so I used something else to assign the color values

    I used

    Select Case Day(Date)
      case 11
         mlngColour = blue


    If you are still having problems let me know, happy to help.

    take care...


    Author Comment

    Alan, i finally had time to put it all together and run it. it looks soooo beautiful! I wish you could see it.

    thanks so much for all the help but most of all your patience and profesionalism.


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Database tuning – How to start and what to tune. This question is frequently asked by many people, both online and offline. There is no hard and fast rule-of-thumb for performance tuning, however, before beginning the tuning process one should a…
    CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    745 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

    19 Experts available now in Live!

    Get 1:1 Help Now