Previewing Reports

That sound of your printer warming up means you forgot the acViewPreview flag again. You're better off avoiding DoCmd.OpenReport entirely.

Previewing Reports

There are several things that annoy me about the default behavior of DoCmd.OpenReport.  To address these annoyances, I built a replacement function that I named PreviewReport().  

In this series of articles, I will take you through the evolution of this function as I address each of my frustrations.

Part 1: Auto-Print

The default behavior of DoCmd.OpenReport in Access is to send the report directly to the user's default printer.  In most cases, though, I prefer to preview the report on-screen first.  

I can't tell you how many times as a young developer I called that method and forgot to pass the acViewPreview flag.  

I would click the button on my form to preview the report.  Nothing would appear on the screen.  After several seconds I would get curious.  This report isn't based on a complex query, I would think; it should be open by now.  Another second or two and the whirring of the fan on the laser printer would alert me to my mistake.  I would quick run over to pull out the paper tray before all 50 pages of the report came shooting out.

This default behavior bit me so many times at the beginning of my Access development career that I finally gave up trying to remember to include the acViewPreview flag altogether.  Instead, I wrapped the DoCmd.OpenReport method inside a convenience function that I called PreviewReport().

The Code

Function PreviewReport(RptName As String, Optional Where As String = "")
    DoCmd.OpenReport RptName, acViewPreview, , Where
End Function
The humble beginnings of the PreviewReport function

This was just the starting point of my PreviewReport function.  The function would go through several more iterations before arriving at its present-day form.  Check back tomorrow for the next step on the journey.

Image by David Dunmore from Pixabay

All original code samples by Mike Wolfe are licensed under CC BY 4.0