Printing a Microsoft Access Report from Visual Bas

By | 2002-06-01

How to print a Microsoft Access report from within VB. Also, VB 16-bit. (by Jose Garrick)

Original Author: anonomous (or see description)

Code

Access 2.0 can be controlled using DDE, while Access 7.0 and later can be controlled using OLE Automation. In both cases, you are generally limited to what is available as a DoCmd statement/method. I'll assume for the moment that you'll be using one of the 32-bit versions of Access. You first setup a reference to Access in the VB References dialog box. Access 7.0 will show up as "Microsoft Access for Windows 95" and Access 8.0 will be listed as "Microsoft Access 8.0 Object Library".
Once that's done, you can create object variables in your application based on the Access application. This little snippet will open a database, run a report and close the database.

Dim ac As Access.Application
Set ac = New Access.Application
' put the path to your database in here
ac.OpenCurrentDatabase("c:foofoo.mdb")
' by default, the OpenReport method of the
' DoCmd object will send the report to the printer
ac.DoCmd.OpenReport "MyReport"
' close the database
ac.CloseCurrentDatabase

That's about all it takes. Just remember that you need to design the reports so that they can be run unattended. Watch for query prompts, message boxes, etc., in the report design or the code behind the report.

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.