ACC2: Using Access Basic Code to Collate and Print Two Reports

ID: Q132241


The information in this article applies to:


SUMMARY

This article describes a method that you can use to collate and print two reports. This method alternates two reports between the DoCmd SelectObject and DoCmd Print macro actions.

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

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:

Q145777 ACC95: Microsoft Access Sample Reports Available on MSL

Q175072 ACC97: Microsoft Access 97 Sample Reports Available on MSL


MORE INFORMATION

At times, you may want to print page 1 of one report and then collate that page with page 1 of another report. Opening the reports and printing each page individually requires the underlying query to run each time. An alternative method is to incorporate both reports in a main/sub report.

To collate and print two reports and to test the results, follow these steps:

  1. Open the sample database NWIND.MDB.


  2. Create a new module and type the following lines in the Declarations section:
    
        '****************************************************************
        'Declarations section of the module
        '****************************************************************
    
          Option Compare Database
          Option Explicit 


  3. Type the following function:
    
        '**********************************************************************
        'NumPages is the number of pages in the largest report. If one report
        'has fewer pages, the DoCmd Print statement for the smaller report runs
        'correctly and no additional pages are printed.
        '********************************************************************** 
    
          Function CollateReports(NumPages, Rpt1 as String, Rpt2 as String)
    
          Dim MyPageNum As Integer
    
          ' Set the page number loop and alternate printing the report pages.
    
          For MyPageNum = 1 to NumPages
    
          ' NumPages is the number of pages to print.
    
             DoCmd SelectObject A_REPORT, Rpt1, True
             DoCmd Print A_PAGES, MyPageNum, MyPageNum
             DoCmd SelectObject A_REPORT, Rpt2, True
             DoCmd Print A_PAGES, MyPageNum, MyPageNum
    
          Next MyPageNum
    
          End Function 


  4. To test the results, type the following line in the Immediate Window, and then press ENTER:

    ? CollateReports(1, "Sales summaries", "Sales Totals by amount")

    Note that Microsoft Access prints one page from each report.



REFERENCES

Microsoft Access "Language Reference," version 2.0, "Print Action," pages 375-376

Microsoft Access "Language Reference," version 2.0, "SelectObject Action," pages 431-432


Keywords          : kbprint PtrOpt 
Version           : 2.0
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: April 23, 1999