Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 24th, 2005, 07:16 AM
Authorized User
 
Join Date: Dec 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default Colouring records

Hello all,
I have created a report under the form of a word table and I wish to colour the background of one record in grey and the second one in white... and so on.
How can I do this?
Thanks,
Sven

 
Old March 24th, 2005, 11:48 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Add a variable to the module behind the report.
Using a toggling mechanism
Code:
    If A = True Then
        A = False
    Else
        A = True
    End If
    In with this toggling mechanism, set the detail's background color.

I believe all this goes into the On_Format event.
 
Old March 25th, 2005, 08:48 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Egads, what was that code about?

1. Create the report. Make your detail section just one line high, otherwise the alternating gray bars will be too tall.

2. Set every control in the detail section to BackStyle = Transparent. Just rope them all and select this property (so the gray will show through).

3. Edit the report's module by adding this to the declaration area(in design view click View>Code, and then put this in General Declarations):
'=====
Dim blnShade As Boolean
'=====

4. Add this code to the report's OnPrint event (not the detail section's OnFormat event):

'=====
Private Sub Detail1_Print(Cancel As Integer, PrintCount As Integer)
Dim lngGray As Long
lngGray = RGB(221, 221, 221)

If blnShade Then
   Me.Detail1.BackColor = lngGray
Else
   Me.Detail1.BackColor = vbWhite
End If

blnShade = Not blnShade
End Sub
'=====

HTH


mmcdonal
 
Old March 25th, 2005, 08:59 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Sorry, in instruction 4 that is add the code to the Detail section's OnPrint event, the report doesn't have one, but the code shows it's the detail section.

mmcdonal
 
Old March 25th, 2005, 09:05 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Ooops, sorry agan, the code should be:

'...
If blnShade = False Then
'...

HTH some more.

mmcdonal
 
Old March 25th, 2005, 09:10 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

In the report's header section's ON FORMAT event, put

Me.Detail.BackColor = RGB(220, 220, 220)

In the report's detail section's ON FORMAT event, put

If Me.Detail.BackColor = vbWhite Then
    Me.Detail.BackColor = RGB(220, 220, 220)
Else
    Me.Detail.BackColor = vbWhite
End If

Note that your detail section may not be called Detail. It could be Detail0, Detail1, etc. so make sure in those cases you use Me.Detail0.BackColor, etc.


Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old March 25th, 2005, 01:04 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Hey mmcdonal: Why would you set things up to re-initialize lngGray on every row?
If you are married to the idea of using a variable, why wouldn't you Dim it in the declaration section, and initialize it only once, when the report initially opens?
But even better, why wouldn't you just make it a Constant?

As long as you have an If block that alternates the If part and the Else part, why wouldn't you put blnShade = False in one part, and blnShade = True in the other?

My post was not meant to be exhaustive, only to point in the right direction. My suggestion is similar to yours (I had forgotten the proper event to put it into—I've been away from Access for awhile), using an if / else and a variable to alternate the rows; I was in a hurry...
(“Egads”? Haven't heard that in awhile...)
 
Old March 25th, 2005, 01:27 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Relax there big fella. I didn't mean to offend. We geeks in the office here are always ribbing one another.

Yours are all very good points. I am going to try them out. I especially like the constant idea. I like SerranoG's ideas as well.

It just goes to show that there are many ways to skin a cat in VBA.


mmcdonal
 
Old March 29th, 2005, 09:58 AM
Authorized User
 
Join Date: Dec 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks guy for your answers...
It works very well and gives a cool effect!
Sven






Similar Threads
Thread Thread Starter Forum Replies Last Post
Colouring first record of report [Solved] Svenzardda Access VBA 1 July 18th, 2007 06:44 AM
problem in updating records & finding records naveed77 VB Databases Basics 1 January 16th, 2007 12:12 PM
problem in updating records & finding records naveed77 VB How-To 1 January 16th, 2007 12:10 PM
cell colouring EXCEL crmpicco Excel VBA 2 April 20th, 2005 11:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.