• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • Last Modified:

in report,field background color based on field value

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
Else
 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
Else
 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
Else
 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
Else
 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
Else
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
Else
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
0
lallentx
Asked:
lallentx
  • 3
  • 2
1 Solution
 
Alan WarrenCommented:
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




Alan

0
 
lallentxAuthor Commented:
Alan,

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
lallentx
0
 
Alan WarrenCommented:
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

Alan
0
 
Alan WarrenCommented:
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

etc...

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

take care...


Alan
0
 
lallentxAuthor Commented:
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.

Lallentx
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now