ACC2000: How to Use Automation to Print Microsoft Access Reports

ID: Q210132


The information in this article applies to:
  • Microsoft Access 2000

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

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


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, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp


MORE INFORMATION

CAUTION: Following the steps in this example will modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and perform 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.

Retail Version of Microsoft Access

  1. Open Microsoft Access.


  2. Create a new, blank database.


  3. 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 


  4. Type the following procedure:


  5. 
    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
     
  6. To test this function, type the following line in the Immediate window, and then press ENTER:


  7. 
    ?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.

Run-Time Version of Microsoft Access

The run-time version of Access only supports the Automation GetObject() function.

  1. Open Microsoft Access.


  2. Create a new database.


  3. Create a module and type the following in the Declarations section:


  4. 
    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 
  5. Type the following procedure:


  6. 
    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 
  7. To test this function, type the following line in the Immediate window, and then press ENTER:


  8. 
    ?OLEOpenReportRuntime("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.

Additional query words: VB OLE Controlling Server

Keywords : kbole kbusage kbdta AccCon RptOthr
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbinfo


Last Reviewed: November 13, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.