Access VBA to Change colour of cells in Report

Owain

New Member
Joined
Jun 9, 2004
Messages
34
Hi,

I'm trying to create a report within Microsoft Access 2000 and I would like to change the background colour of a box within a report based on the value returned.

The box is named "Progress_Percentage_Complete". Values in this box can range from 0 to 100. I am wanting the background colour to change as follows.

If value is 0 to 39 = red
40 to 59 = orange
60 to 79 = yellow
80 to 99 = light green
100= dark green

I gather that this can be done in VBA. However, I have very little experience in this area and wondered if anyone knows what code I would use to achieve the colour coding as detailed above. Also, where would I place this code within the report. Will it be found somewhere in the 'properties' of the Progree_Percentage_Complete Box?

Any help you can offer will be greatly appreciated!

Many thanks,

Owain.
:confused:
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Owain

Could you not use Conditional Formatting?

It's available in Access 2000 but I'm afraid it's limited, like Excel, to 3 conditions.
 
Upvote 0
Hi Norie - I'm afraid I need 5 different colours so conditional formatting doesn't do it for me.

Thanks for the suggestion though.

Cheers,

Owain.
 
Upvote 0
In your report, go to the Properties for Progress_Percentage_Complete and make sure in the Format tab that Back Style is Normal
Now select the properties of the Detail section, and in the Events tab, double-click the blank On Format line. You'll see Event Procedure. Click the Builder (...) button to go to the code module, and paste this:
Code:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Select Case [Progress_Percentage_Complete]
    Case Is < 40
        [Progress_Percentage_Complete].BackColor = 255 'red
    Case Is < 60
        [Progress_Percentage_Complete].BackColor = 52479 'orange
    Case Is < 80
        [Progress_Percentage_Complete].BackColor = 65535 'yellow
    Case Is < 100
        [Progress_Percentage_Complete].BackColor = 65280 'light green
    Case 100
        [Progress_Percentage_Complete].BackColor = 32896 'dark green
End Select
End Sub
Save the report and run it.

Denis
 
Upvote 0
I'm trying to do something very similar to what the below code is doing expect that I need to look up something 1st. Basically, if WorkFuntion in my report equals CARTPICK then I want to follow the same conditional formating below. If Workfunction does not equal CARTPICK then I want my conditional formating to follow a different number range (i.e. <20 green, <40 blue, <60 black). Does this make sense?

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Select Case [Progress_Percentage_Complete]
Case Is < 40
[Progress_Percentage_Complete].BackColor = 255 'red
Case Is < 60
[Progress_Percentage_Complete].BackColor = 52479 'orange
Case Is < 80
[Progress_Percentage_Complete].BackColor = 65535 'yellow
Case Is < 100
[Progress_Percentage_Complete].BackColor = 65280 'light green
Case 100
[Progress_Percentage_Complete].BackColor = 32896 'dark green
End Select
End Sub
 
Upvote 0
Hi afs, you could use a nested Case statement. Like...
Code:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Select Case [WorkFunction]
    Case "CARTPICK"
        Select Case [Progress_Percentage_Complete]
            Case Is < 40
                [Progress_Percentage_Complete].BackColor = 255 'red
            Case Is < 60
                [Progress_Percentage_Complete].BackColor = 52479 'orange
            Case Is < 80
                [Progress_Percentage_Complete].BackColor = 65535 'yellow
            Case Is < 100
                [Progress_Percentage_Complete].BackColor = 65280 'light green
            Case 100
                [Progress_Percentage_Complete].BackColor = 32896 'dark green
        End Select
    Case Else
        Select Case [Progress_Percentage_Complete]
            Case Is < 20
                [Progress_Percentage_Complete].BackColor = 255 'red
            Case Is < 40
                [Progress_Percentage_Complete].BackColor = 52479 'orange
            Case Is < 60
                [Progress_Percentage_Complete].BackColor = 65535 'yellow
            Case Is < 100
                [Progress_Percentage_Complete].BackColor = 65280 'light green
            Case 100
                [Progress_Percentage_Complete].BackColor = 32896 'dark green
        End Select
End Select
End Sub
You'll need to insert the new color codes for the second set. I got the values by picking colours for a textbox, then checking the Backcolor property.

Denis
 
Upvote 0
This code works perfectly. However, I actually have 5 different Workfunctions that may come up (i.e. CARTPICK, FULLPICK, PUTAWAY, RECEIVING, REPLENISHMENT). Each workfunction has a differnt productivity measurement. Can you provide me with an example with the one previously except that I want to look up 5 different workfuctions? thanks
 
Upvote 0
The Select statement can be expanded as much as you need. This section...
Code:
    Case "CARTPICK"
        Select Case [Progress_Percentage_Complete]
            Case Is < 40
                [Progress_Percentage_Complete].BackColor = 255 'red
            Case Is < 60
                [Progress_Percentage_Complete].BackColor = 52479 'orange
            Case Is < 80
                [Progress_Percentage_Complete].BackColor = 65535 'yellow
            Case Is < 100
                [Progress_Percentage_Complete].BackColor = 65280 'light green
            Case 100
                [Progress_Percentage_Complete].BackColor = 32896 'dark green
        End Select
...can be repeated with copy and paste, just before the Case Else bit. Then go through, changing CARTPICK to one of the other four categories, and setting your cutoffs as required.

Denis
 
Upvote 0
All,
I am new to Access macros...
I am trying to change color of values in one column of my report [Q1] ($ field) based on another column [StatusT] (text).

[StatusT] values are as follows:
-Red
-Green
-Blue
-Black

[Q1] values: any $ currency value

Sample Output should be:
If [StatusT] = Red then value in [Q1] is painted Red

After careful analysis of above macros this is what I came up with for my case.
As a result I do not get any errors but it does not colour my values either.
Please help,

Code:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Select Case [StatusT]
   Case Is = Red
        [Q1].BackColor = 255 'red
    Case Is = Blue
        [Q1].BackColor = 16711680 'blue
    Case Is = Black
        [Q1].BackColor = 0 'black
    Case Is = Green
        [Q1].BackColor = 32768 'green
End Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,225
Messages
6,123,732
Members
449,116
Latest member
Aaagu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top