Use OLE Automation to Print Access Reports

Use OLE Automation to Print Access Reports

You can print canned reports in an Access database from VB in different ways. I created this routine to print any report with any criteria or filter from any database. It opens Access with the user- or program-controlled database, then opens the report in the mode specified. In order for this to work with Access constants, you must select Access from the References dialog box (accessed from References under the Tools menu in VB4 and under the Project menu in VB5).

The PrintReport subroutine has the same parameters as the Access Docmd.OpenReport command with the exception of DBPath. I didn’t use IsMissing to test for missing parameters because Access handles it for you, but you can add it to supply your own default values for any missing parameters. Note: In VB4, any optional parameter must be a Variant data type. When you want to print a report from code, call the routine like this:

 	PrintReport MyDBPath, MyReportName, acPreview, _		MyCriteriaSub PrintReport(ByVal DBPath As String, ByVal ReportName _	As StringOptional OpenMode As Integer, Optional Filter As String, _	OptionalCriteria As String)	Dim appAccess As Object	Set appAccess = CreateObject("Access.Application")	appAccess.OpenCurrentDatabase (DBPath)'********************************************************	'Access constants for OpenMode are	'acNormal  - Print (default)	'acPreview - Print Preview	'acDesign  - Design Edit Mode    '********************************************************	appAccess.DoCmd.OpenReport ReportName, OpenMode, _		FilterNameCriteria'********************************************************	'if open mode is Preview then don't quit Access this can	'also be deleted if you do not want Access to quit after	'printing a report'********************************************************	If OpenMode <> acPreview Then		appAccess.Quit	End If	Set appAccess = NothingEnd Sub
See also  Essential Measures for Safeguarding Your Digital Data

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist