Changing Background Color for each 5 rows

K

Ken Kazinski

How do you set the background color for each 5 rows of data in a group?

I think I might be able to use the on print event, but that did not seem to
work correctly.

Thanks in advance.
 
A

Allen Browne

You want 5 rows with a white background, and then 5 rows with a grey
background?

1. Add a text box to the report, and give it these properties:
Control Source =1
Running Sum Over All
Format General Number
Name txtCount

2. Set the On Format property of the Detail section to:
[Event Procedure]

3. Click the Build button (...) beside this property.
Between the Private Sub... and End Sub lines enter something like this:

Dim lngColor As Long
Const lngcNormal = &HFFFFFF
Const lngcHighlight = &HCCCCCC
Const lngcRowSwap As Long = 5

If (((Me.txtCount - 1&) \ lngcRowSwap) Mod 2 = 0 Then
lngColor = lngcNormal
Else
lngColor = lngcHighlight
End If

With Me.Section(acDetail)
If .BackColor <> lngColor Then
.BackColor = lngColor
End If
End With

You may also need to change the BackStyle property of the text boxes to
transparent.

Adjust the colors to what you need.
 
K

Ken

Hi Allen,

Thank you - looks like it will work. Is there a way to count the rows on
the detail without creating a field?

Thanks.


Allen Browne said:
You want 5 rows with a white background, and then 5 rows with a grey
background?

1. Add a text box to the report, and give it these properties:
Control Source =1
Running Sum Over All
Format General Number
Name txtCount

2. Set the On Format property of the Detail section to:
[Event Procedure]

3. Click the Build button (...) beside this property.
Between the Private Sub... and End Sub lines enter something like this:

Dim lngColor As Long
Const lngcNormal = &HFFFFFF
Const lngcHighlight = &HCCCCCC
Const lngcRowSwap As Long = 5

If (((Me.txtCount - 1&) \ lngcRowSwap) Mod 2 = 0 Then
lngColor = lngcNormal
Else
lngColor = lngcHighlight
End If

With Me.Section(acDetail)
If .BackColor <> lngColor Then
.BackColor = lngColor
End If
End With

You may also need to change the BackStyle property of the text boxes to
transparent.

Adjust the colors to what you need.

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

Ken Kazinski said:
How do you set the background color for each 5 rows of data in a group?

I think I might be able to use the on print event, but that did not seem
to
work correctly.

Thanks in advance.
 
A

Allen Browne

Set the Visible property of the text box to No if you don't wish to see it.

The text box accumulating the value is the only reliable way to get a count
that spreads over multiple pages. If you use code in the report events to
assign a value to a variable, the events may not fire for pages that are not
previewed/printed, so the numbers are wrong.

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

Ken said:
Hi Allen,

Thank you - looks like it will work. Is there a way to count the rows on
the detail without creating a field?

Thanks.


Allen Browne said:
You want 5 rows with a white background, and then 5 rows with a grey
background?

1. Add a text box to the report, and give it these properties:
Control Source =1
Running Sum Over All
Format General Number
Name txtCount

2. Set the On Format property of the Detail section to:
[Event Procedure]

3. Click the Build button (...) beside this property.
Between the Private Sub... and End Sub lines enter something like this:

Dim lngColor As Long
Const lngcNormal = &HFFFFFF
Const lngcHighlight = &HCCCCCC
Const lngcRowSwap As Long = 5

If (((Me.txtCount - 1&) \ lngcRowSwap) Mod 2 = 0 Then
lngColor = lngcNormal
Else
lngColor = lngcHighlight
End If

With Me.Section(acDetail)
If .BackColor <> lngColor Then
.BackColor = lngColor
End If
End With

You may also need to change the BackStyle property of the text boxes to
transparent.

Adjust the colors to what you need.

Ken Kazinski said:
How do you set the background color for each 5 rows of data in a group?

I think I might be able to use the on print event, but that did not
seem
to
work correctly.

Thanks in advance.
 
K

Ken

Hi Allen,

I tried the code you provided and I am not getting the results I expected.

I took the code and tried to modifiy it. My goal was to have the 5th row of
each group highlighted.

I am curious what the "- 1&) \" does?

Also the code does not change the color of any of the rows.
 
A

Allen Browne

The backslash does integer division (losing the remainder.)

The counter starts at 1. You need it to to start from zero, hence -1.

The ampersand is optional. It just specifies that the 1 is a Long.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top