When formatting reports, you should never rely on a sequence of event calls.
For one thing, what if you're previewing and going backward through the pages?
Some events will actually be firing in backward order in that case.
Now, the reason your code doesn't seem to be doing anything at all, not even
working strangely because of the issue I previously stated is that the Format
event actually fires at least twice for each section, possibly more if it
tries to format it on one page, figures it won't fit, then tries again on the
next page. The FormatCount parameter tells you how many times the event has
fired, so you can check for an even numbered value to perform the operation.
Now, to get away from code dependent upon a firing sequence, you need a way to
identify the actual row number you're on. You can do this by creating a
hidden counter control on the report. You do that by making a hidden control,
setting its control source to =1 (yes, the equal sign is part of what you
type), then you set the Running Sum property of the control to Over Report.
now, you have a running sum of the constant value 1 over the report which
amounts to a running count which amounts to a row number.
Assuming that control is called txtRowCounter, in your code, you can say If
Me!txtRowCounter mod 2 = 1 Then <odd-row-action> Else <even-row-action>.
On Fri, 06 Feb 2004 10:47:13 +0100, Georges Heinesch <ne**@geohei.lu> wrote:
Hi.
I have a problem with alternating background colors in a report. The
code below shows, that the first line should always be white (default
global boolean value is false). However it depends on the number of
records upon which the report is based. If the number of records is odd,
the first line is grey; if it's even, the first line is white (how it
should be).
I included so much debug.print lines to nail down the source of the
problem, but I didn't find one single hint why Access behaves like that.
All seems properly programmed and executed, but the lines show the wrong
background color when the number of records is odd.
Any (!) ideas ... !
TIA
---- cut here ----
Option Compare Database
Option Explicit
Dim boolColor As Boolean
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Call ChangeColor
End Sub
Private Sub ChangeColor()
Const conColorWhite = 16777215
Const conColorGrey = 14737632
If boolColor Then
Me.Section(0).BackColor = conColorGrey
Else
Me.Section(0).BackColor = conColorWhite
End If
boolColor = Not boolColor
End Sub
---- cut here ----