473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Alternate backcolor in report

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 ----

--
Georges
Nov 12 '05 #1
7 4515
One issue with your code is that the Format event of the Detail section can
be called multiple times for one row where pages break or things are growing
or shrinking or being kept together or ... Checking FormatCount might be a
partial solution, or you might be able to use the Print event since you are
not changing the size of anything.

Assuming Access 2000 or later, it would be much more efficient to do this
with conditional formatting.

1. Place a text box in the detail section, and set these properties:
Name txtCount
Control Source =1
Running Sum Over All
Visible No

2. Place another text box in the detail section. Send To Back so it sits
behind the others. Select this textbox, and choose Conditional Formatting on
the Format menu. Set the first condition to this Expression:
[txtCount] Mod 2 = 1
and choose the grey color in the bucket.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Georges Heinesch" <ne**@geohei.lu> wrote in message
news:40********@news.vo.lu...
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 ----

--
Georges

Nov 12 '05 #2
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 ----


Nov 12 '05 #3
Allen Browne wrote:
One issue with your code is that the Format event of the Detail section can
be called multiple times for one row where pages break or things are growing
or shrinking or being kept together or ... Checking FormatCount might be a
partial solution, or you might be able to use the Print event since you are
not changing the size of anything.
I included some debug.print line to check FormatCount, but it always
showed 1.
Assuming Access 2000 or later, it would be much more efficient to do this
with conditional formatting.

1. Place a text box in the detail section, and set these properties:
Name txtCount
Control Source =1
Running Sum Over All
Visible No
That is done anyway already since I use a control to count the rows.
2. Place another text box in the detail section. Send To Back so it sits
behind the others. Select this textbox, and choose Conditional Formatting on
the Format menu. Set the first condition to this Expression:
[txtCount] Mod 2 = 1
and choose the grey color in the bucket.


What about omiiting our suggested textbox, and including the code into
Detail_Format? This avoids creating this text box control.

Any objections?

---- cut here ----
Option Compare Database
Option Explicit

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

Const conColorWhite = 16777215
Const conColorGrey = 14737632

' change background color
If Me!txtRowCounter Mod 2 = 0 Then
Me.Section(0).BackColor = conColorGrey
Else
Me.Section(0).BackColor = conColorWhite
End If

End Sub
---- cut here ----

This works perfectly now!
Thanks a lot so far!

TIA

--
Georges
Nov 12 '05 #4
Steve Jorgensen wrote:
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?
I have only sone records at the moment (test data). These don't fit a
complete page yet.
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.
I tested the FormatCount variable with a debug.print line, but it always
showed 1 for some reason?!
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.
Done!
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>.


This code should be within the Detail_Format part of the report, correct?

TIA

--
Georges
Nov 12 '05 #5
On Fri, 06 Feb 2004 16:25:20 +0100, Georges Heinesch <ne**@geohei.lu> wrote:
Steve Jorgensen wrote:
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?
I have only sone records at the moment (test data). These don't fit a
complete page yet.
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.


I tested the FormatCount variable with a debug.print line, but it always
showed 1 for some reason?!


Perhaps, my information is out of date. The statement was true for Access 97
and earlier.
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.


Done!
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>.


This code should be within the Detail_Format part of the report, correct?


Yes. In fact, given the uncertain firing order, the only place you should
usually set any dynamic formatting is in an event handler for the section that
will be affected.
Nov 12 '05 #6
The Running Sum is a simple, reliable count.

The Detail_Format event can occur multiple times, and you may need to handle
the Retreat event as well.

Further, VBA code will execute more slowly than conditional formatting.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Georges Heinesch" <ne**@geohei.lu> replied in message
news:40********@news.vo.lu...

What about omiiting our suggested textbox, and including the code into
Detail_Format? This avoids creating this text box control.

Any objections?

Nov 12 '05 #7
Steve Jorgensen wrote:
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>.


This code should be within the Detail_Format part of the report, correct?


Yes. In fact, given the uncertain firing order, the only place you should
usually set any dynamic formatting is in an event handler for the section that
will be affected.


Thanks a lot.
Works perfectly now!

--
Georges
Nov 12 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: news | last post by:
Hi all, How do you implement alternate line colors in ms access datasheet view Regards Joe --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system...
1
by: jeffgeorge | last post by:
Using a bit of code to produce alternate line shading in a report. Seems it should work for a form as well but no luck. Does anyone have any ideas what I need to change or if it is even possible...
1
by: Matt | last post by:
I am wanting to have the backcolor of some combo boxes change acording to what value is in the combo boxes in a report. The report can be one page to multiple pages with the same combo boxes on...
3
by: buck | last post by:
reports/ can we alternate formatting such as "no shading" and then "shading" for multiple records such as a phone directory in Access2000 reports
1
by: Jim Heavey | last post by:
I am unable to get the data list to render the AlternateStyle.... Any ideas as to why? <Table title="Calendar Maintenance" > <asp:datalist id="dlCalendar" runat="server"...
5
by: Michael R | last post by:
Searching the net I've found a simple technique to add row numbers and alternate colors (for the even and the uneven row) to a continuous form. 1st step: Create a textbox, send it to background...
0
by: Wolfgrin | last post by:
Hi, I have set up a report In Access 2003 where there are 2 check boxes I refer to to change the color of the Date of that record. That part is working. I also need to Change the color of the...
1
by: strychtur | last post by:
Hi All, Does any one know how to alternate item backcolor of a checkedlistbox? Is it even possible? Thanks for your help Cheers strychtur
3
by: Charles Law | last post by:
Ok. I haven't been around for a while, so I hope someone will come to my rescue. I thought I had done this years ago, but I have scoured my code snippets and can't find it. I want my ListView...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.