Microsoft KB Archive/210132

= HOW TO: Use Automation to Print Microsoft Access Reports in Access 2000 =

Article ID: 210132

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q210132



Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

IN THIS TASK
SUMMARY
 * Retail Version of Microsoft Access
 * Run-Time Version of Microsoft Access

REFERENCES



SUMMARY
By carrying out the OpenReport method of the DoCmd object, you can print Access reports from any Automation client application, such as Microsoft Visual Basic or the Microsoft Visual Basic Environment (VBE). This enables you to implement Access as the reporting component in your application solutions.

NOTE: This article explains a technique demonstrated in the sample file, RptSmp00.mdb. For information about how to obtain this sample file, please see the following article in the Microsoft Knowledge Base:

231851 ACC2000: Microsoft Access 2000 Sample Reports Available in Download Center

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.

NOTE: The following code is designed for the retail version of Access. If you are using the run-time version of Access, please refer to the "Run-time Version of Microsoft Access" section later in this article.

back to the top

Retail Version of Microsoft Access
 Open Microsoft Access. Create a new, blank database.  Create a module and type the following in the Declarations section: Option Explicit ' In other applications like Microsoft Visual Basic, ' you can include a reference to Microsoft Access to ' gain the use of Access constants. Or, use the following ' constant values... ' Global Const acNormal = 0 ' Global Const acDesign = 1 ' Global Const acPreview = 2 ' - ' Application Quit options... ' saves all objects without displaying a dialog box: ' Global Const acSaveYes = 0 ' displays a dialog box that asks whether you want to save any ' database objects that have been changed but not saved: ' Global Const acPrompt = 1 ' quits Microsoft Access without saving any objects: ' Global Const acExit = 2   Type the following procedure: Function OLEOpenReport(strDBName As String, _                      strRptName As String, _                       Optional ByVal intDisplay As Variant, _                       Optional ByVal strFilter As Variant, _                       Optional ByVal strWhere As Variant) As Boolean

On Error GoTo OLEOpenReport_Err

' Create Automation object. Dim objAccess As Object Set objAccess = CreateObject("Access.Application")

' Open the supplied database. ' Optional parameter at the end of statement ' indicates exclusive mode if set to True... objAccess.OpenCurrentDatabase strDBName, False

' The OpenReport method uses the following arguments...  ' Report Name - Name of the report object. ' View - Display in Print Preview or send to printer. '       acNormal - Print report '       acDesign - open report in design (n/a in runtime) '       acPreview - open in preview window ' Filter Name - Name of a saved filter query. ' Where Condition = valid SQL where condition.

If IsMissing(intDisplay) Then intDisplay = acNormal If IsMissing(strFilter) Then strFilter = "" If IsMissing(strWhere) Then strWhere = "" objAccess.DoCmd.OpenReport strRptName, intDisplay, strFilter, _ strWhere

' Close Microsoft Access session instance... objAccess.Quit acExit

Set objAccess = Nothing OLEOpenReport = True OLEOpenReport_End: Exit Function

OLEOpenReport_Err: MsgBox Error$, vbInformation, "Automation" Resume OLEOpenReport_End End Function

  To test this function, type the following line in the Immediate window, and then press ENTER: ?OLEOpenReport("c:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb", "Invoice", strWhere:="OrderId = 10251") 

The function opens the Northwind database, opens the Invoice report, sets the recordset to OrderId# 10251, and prints the report to the printer.

NOTE: Make sure to provide appropriate paths to the files on your system.

back to the top

Run-Time Version of Microsoft Access
The run-time version of Access only supports the Automation GetObject function.

 Open Microsoft Access.</li> Create a new database.</li>  Create a module and type the following in the Declarations section: Option Explicit ' In other applications like Microsoft Visual Basic, ' you can include a reference to Microsoft Access to ' gain the use of Microsoft Access constants. Or, use the following ' constant values... ' Global Const acNormal = 0 ' Global Const acDesign = 1 ' Global Const acPreview = 2 ' - ' Application Quit options... ' saves all objects without displaying a dialog box: ' Global Const acSaveYes = 0 ' displays a dialog box that asks whether you want to save any ' database objects that have been changed but not saved: ' Global Const acPrompt = 1 ' quits Microsoft Access without saving any objects: ' Global Const acExit = 2 </li>  Type the following procedure: Function OLEOpenReportRuntime(strDBName As String, _ strRptName As String, _ Optional ByVal intDisplay As Variant, _ Optional ByVal strFilter As Variant, _ Optional ByVal strWhere As Variant _ ) As Boolean

On Error GoTo OLEOpenReportRuntime_Err

Dim x As Long Dim objAccess As Object

' Open the run-time instance and database... ' -- ' The use of the Chr$(34) function supplies ' quotation marks around the database name which is ' required by Shell when the optional command ' line parameter contains spaces...

x = Shell("c:\myapp\Office\msaccess.exe " &_ Chr$(34) & strDBName & Chr$(34) & _ "/Runtime /Wrkgrp " & Chr$(34) & _ "c:\myapp\system.mdw" & Chr$(34))

Set objAccess = GetObject(strDBName)

' The OpenReport method uses the following arguments... ' Report Name - Name of the report object. ' View - Display in Print Preview or send to printer. '       acNormal - Print report '       acDesign - open report in design (n/a in runtime) '       acPreview - open in preview window ' Filter Name - Name of a saved filter query. ' Where Condition = valid SQL where condition.

If IsMissing(intDisplay) Then intDisplay = acNormal If IsMissing(strFilter) Then strFilter = "" If IsMissing(strWhere) Then strWhere = "" objAccess.DoCmd.OpenReport strRptName, intDisplay, strFilter, _ strWhere

' Close Microsoft Access session instance... objAccess.Quit acExit

Set objAccess = Nothing OLEOpenReportRuntime = True OLEOpenReportRuntime_End: Exit Function

OLEOpenReportRuntime_Err: MsgBox Error$, vbInformation, "Automation" Resume OLEOpenReportRuntime_End End Function </li>  To test this function, type the following line in the Immediate window, and then press ENTER: ?OLEOpenReportRuntime("c:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb", "Invoice", strWhere:="OrderId = 10251") </li></ol>

The function opens the Northwind database, opens the Invoice report, sets the recordset to OrderId# 10251, and prints the report to the printer.

NOTE: Make sure to provide appropriate paths to the files on your system.

back to the top

<div class="references_section">