|[ Team LiB ]|
Recipe 3.12 Alternate Gray Bars on My Reports
You have some reports on which you'd like to print alternate rows with gray bars in the background. Printing these bars makes the reports easier to read, especially when there's lots of data or the report is very wide. Is there a way to create these bars in Access?
There are a number of ways to print alternate rows with gray and white backgrounds. The simplest method is to alternate the background color of the detail section for each new record. This solution shows you how to use this method to achieve the desired effect on your reports.
To create your own reports with alternating gray bars in the detail section, follow these steps:
Now load 03-12.MDB and open the rptGrayBar report in preview view. This report may not look very good on your screen (it depends on the screen resolution and the color depth of your screen driver), but printed it will look something like the report shown in Figure 3-29. (The exact output will depend on your printer; you may need to modify the color setting for the gray bar to optimize it.)
The code shown in Step 4 relies on a module-level variable, blnShade, that alternates between True and False. If you followed the instructions for Step 5, you set the value of blnShade to a particular value every time you print the page header (before any rows are printed on that page). From then on, every time Access prints the detail section, it decides what to do based on the value in blnShade. What's more, every time it prints the detail section, it alternates the value of blnShade using this line of code:
blnShade = Not blnShade
That is, if blnShade was False, now it will be True, and vice versa.
Once the code has decided whether to shade the section, it sets the background color to the color value of gray or white, based on the value of blnShade, using the following If...Then...Else statement:
If blnShade Then Me.Detail1.BackColor = acbcColorGray Else Me.Detail1.BackColor = vbWhite End If
We used the built-in VBA constant for white, but there is no constant for gray, so we defined a value corresponding to the color gray earlier in the procedure, using the built-in VBA function, RGB. An easy way to determine the numeric values for colors is by selecting a section or a control in design view and using the color palette to set the desired color. Then you can read the color value off of the properties sheet. Another option is to use vbGreen, which looks good when previewing the report and also results in a pleasing gray color when printed on a black-and-white printer.
|[ Team LiB ]|