Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Highlighting Alternate Rows In a Report

Posted on 2008-11-14
Medium Priority
Last Modified: 2013-11-28
I have tried several different options for highlighting alternate rows in my reports.  I can't seem to make any stick.  Then, I came accross the following code on Microsofts site:
Option Compare Database
Option Explicit

Private shadeNextRow As Boolean
Const shadedColor = 12632256
' Const shadedColor = 15726583 ' alternative shade colors
' Const shadedColor = 14078404
' Const shadedColor = 13356495
' Const shadedColor = 14281974
Const normalColor = 16777215

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

  On Error GoTo Detail_Format_Error

' Choose a color based on the shadeNextRow value
  If shadeNextRow = True Then
    Me.Section(acDetail).BackColor = shadedColor
    Me.Section(acDetail).BackColor = normalColor
  End If

' Switch the color for the next row
  shadeNextRow = Not shadeNextRow
  Exit Sub

  MsgBox "Error " & Err.Number & ": " & Err.Description
  Resume Detail_Format_Exit

End Sub

Problem is, with this code added, my report will NOT open in print preview, and after I try, I can no longer get into the design view of my report without either deleting the code or closing/reopening the database.   Any ideas on either making the code above work, or an alternative?

Thanks in Advance.
Question by:cebaker75
  • 3
  • 2

Author Comment

ID: 22959711
Forgot Something-  The code I attached does work when the report is printed, but my reports need to open to print preview.  
LVL 11

Accepted Solution

RgGray3 earned 1000 total points
ID: 22960183
One simple way is to use the record counter

you can use a number of schemes to alternate...  including  
If Me.detail.backcolor = vbwhite then
     Me.detail.backcolor = vbwhite
End if

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
    If Me.RecordCounter Mod 2 Then
        Me.Detail.BackColor = 14540253
        Me.Detail.BackColor = vbWhite
    End If
End Sub

Open in new window


Author Closing Comment

ID: 31516765
Wow, that was so much easier and better.  Thank RgGray3!
LVL 11

Expert Comment

ID: 22962903
Glad to be of assistance...

Sometimes things LOOK or SEEM more difficult than they really are...

Sometimes it takes more time to write 1 line of code than it does to write a page....

Always ask yourself ....   there has got to be a simple way to do this......  and often there is!

Good luck
LVL 11

Expert Comment

ID: 22962917

Any release of the Ken Getz "Access Cookbook" books have Lots of these types of examples...
They are well structured and organized with sample databases to illustrate each concept...

Well worth the price of admission

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
When we develop an application in Ms Access 2016 we should also try to protect the queries, macros and table links. I know I may not have a permanent solution but for novice users, they will not manage to break your application. Below is the detail …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question