Microsoft KB Archive/296586

= How to use automation to print reports in Access 2002 =

Article ID: 296586

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Access 2002 Standard Edition

-



This article was previously published under Q296586





For a Microsoft Access 97 version of this article, see 145707.



For a Microsoft Access 2000 version of this article, see 210132.

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. 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 &quot;Run-time Version of Microsoft Access&quot; 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 then 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(&quot;Access.Application&quot;)

' 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 = &quot;&quot; If IsMissing(strWhere) Then strWhere = &quot;&quot; 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, &quot;Automation&quot; Resume OLEOpenReport_End End Function

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

The function opens the Northwind database, opens the Invoice report, sets the recordset to OrderId# 10251, and then 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.

<ol> Open Microsoft Access.</li> Create a new database.</li>  Create a module, and then 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(&quot;c:\myapp\Office\msaccess.exe &quot; &_ Chr$(34) & strDBName & Chr$(34) & _ &quot;/Runtime /Wrkgrp &quot; & Chr$(34) & _ &quot;c:\myapp\system.mdw&quot; & 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 = &quot;&quot; If IsMissing(strWhere) Then strWhere = &quot;&quot; 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, &quot;Automation&quot; Resume OLEOpenReportRuntime_End End Function </li>  To test this function, type the following line in the Immediate window, and then press ENTER: ?OLEOpenReportRuntime(&quot;c:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb&quot;, &quot;Invoice&quot;, strWhere:=&quot;OrderId = 10251&quot;) </li></ol>

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

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

back to the top

Additional query words: OfficeKBHowTo VB OLE Controlling Server inf

Keywords: kbreport kbautomation kbhowtomaster kbprint kbprogramming kbusage KB296586

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.