ACC: How to Use Automation to Print Microsoft Access Reports

ID: Q145707

The information in this article applies to:

SUMMARY

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

By carrying out the DoCmd object's OpenReport method, you can print Microsoft Access reports from any Automation client application, such as the Microsoft Visual Basic Programming System, Microsoft Excel, or Microsoft Project. This enables you to implement Microsoft Access as the reporting component in your application solutions.

NOTE: This article explains a technique demonstrated in the sample files, RptSampl.exe (for Microsoft Access for Windows 95 version 7.0) and RptSmp97.exe (for Microsoft Access 97). For information about how to obtain these sample files, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q145777
   TITLE     : ACC95: Microsoft Access Sample Reports Available on MSL

   ARTICLE-ID: Q175072
   TITLE     : ACC97: Microsoft Access 97 Sample Reports Available on MSL

MORE INFORMATION

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

CAUTION: Following the steps in this example will modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file or perform these steps on a copy of the Northwind database.

NOTE: The following code is designed for the retail version of Microsoft Access. If you are using the run-time version of Microsoft 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:

      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

5. To test this function, type the following line in the Debug window, and
   then press ENTER.

       ?OLEOpenReport("c:\MSOffice\Access\Samples\Northwind.mdb", _
                      "Invoice", strWhere:="OrderId = 10251")

The function will open the Northwind database, open the Invoice report, set the recordset to OrderId# 10251, and print the report to the printer.

NOTE: Make sure to supply the applicable path to files as defined on your system.

Run-Time Version of Microsoft Access

The run-time version of Microsoft Access will only support the use of the Automation GetObject() function.

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

4. 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))

         ' If you are using Microsoft Access 97, msaccess.exe will be
         ' in the Office subfolder of the folder in which you
         ' installed your runtime application.

         ' If you are using Microsoft Access 7.0, use the following:

         ' x = Shell("c:\Program Files\Common Files\Microsoft Shared\" & _
         ' "Microsoft Access Runtime\msaccess.exe " & _
         ' Chr$(34) & strDBName & 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

5. To test this function, type the following line in the Debug window, and
   then press ENTER.

      ?OLEOpenReportRuntime("c:\MSOffice\Access\Samples\Northwind.mdb", _
                             "Invoice", strWhere:="OrderId = 10251")

The function will open the Northwind database, open the Invoice report, set the recordset to OrderId# 10251, and print the report to the printer.

NOTE: Make sure to supply the applicable path to files as defined on your system.

REFERENCES

For more information about Automation, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q147816
   TITLE       ACC: Using Microsoft Access as an Automation Server

For more information about using Microsoft Access as an Automation server, search on the phrase "Servers," and then "Automation" or ask the Microsoft Access 97 Office Assistant.

Additional query words: VB OLE Controlling Server

Keywords          : kbole kbusage RptOthr 
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbinfo

Last Reviewed: November 21, 1998