Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Jul 2, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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, _

Sub PrintReport(ByVal DBPath As String, ByVal ReportName _
	As String
Optional OpenMode As Integer, Optional Filter As String, _
Criteria 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, _

	'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
	End If
	Set appAccess = Nothing

End Sub
James Kahl
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date