ACC: How to Maintain a Print Log for Reports

Last reviewed: April 2, 1997
Article ID: Q154894
The information in this article applies to:
  • Microsoft Access versions 2.0, 7.0, 97

SUMMARY

Moderate: Requires basic macro, coding, and interoperability skills.

This article describes how to add a record to a table each time you print a report. This technique is useful for maintaining a print log that tracks the print history of a report.

MORE INFORMATION

The following example uses the Print event of a Microsoft Access report to add a record to a table. The record contains the report's name and print date. The example uses the report's Activate and Deactivate events to set a global variable that is evaluated during the Print event. This prevents Print Preview from adding a new record to the history table.

NOTE: In Microsoft Access 7.0 and 97, there is one sequence of events that will create a new record in the history table when you Print Preview the report. If you open the report in Design view to view or change any of its code and then switch to Print Preview, the Activate event does not occur, and the global variable is not set. To work around this problem, after viewing or changing the report's code, close the report before you open it in Print Preview.

CAUTION: Following the steps in this example will modify the sample database Northwind.mdb (or NWIND.MDB in version 2.0 or earlier). You may want to back up the Northwind.mdb (or NWIND.MDB) file and perform these steps on a copy of the database.

Creating a History Table and a Report

  1. Open the sample database Northwind.mdb (or NWIND.MDB in version 2.0).

  2. Create a table with the following structure:

          Table: tblPrintedReports
          --------------------------------------------------
          Field Name: ID
    
             DataType: AutoNumber (or Counter in version 2.0)
             Indexed: Yes (No Duplicates)
          Field Name: ReportName
             DataType: Text
             FieldSize: 75
          Field Name: PrintDate
             DataType: Date/Time
             Format:  General Date
    
          Table Properties: tblPrintedReports
          -----------------------------------
          PrimaryKey: ID
    
    

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

          Option Explicit
          Global Flag
    

  4. Save the module as basPrintedReports, and then close it.

  5. Create an AutoReport based on the Customers table. Save the report as rptCustomers.

  6. Open the rptCustomers report in Design view. Set the report's OnActivate property to the following event procedure:

          Private Sub Report_Activate()
          Flag = 0
          End Sub
    

  7. Set the report's OnDeactivate property to the following event procedure:

          Private Sub Report_Deactivate()
          Flag = -1
          End Sub
    

  8. Set the report header's OnPrint property to the following event procedure.

    NOTE: You must include the report header section on the report, and its Height property must be greater than zero. However, you do not have to place any controls in the header section.

          Sub ReportHeader3_Print (Cancel As Integer, PrintCount As Integer)
          Dim dbs As Database, rst As Recordset
             Set dbs = CurrentDB()
             Set rst = dbs.OpenRecordset("tblPrintedReports")
          Flag = Flag + 1
          ' If the current value of Flag = 1, then a hard copy of the
          ' report is printing, so add a new record to the history table.
          If Flag = 1 Then
             rst.AddNew
             rst!ReportName = "rptCustomers"
             rst!PrintDate = Now
             rst.Update
             Flag = 0
          End If
          End Sub
    
    

  9. Save the rptCustomers report, and then close it.

Generating a History Record

  1. Select the rptCustomers report in the Database window.

  2. On the File menu, click Print to print the report.

  3. Open the tblPrintedReports table. Note that a new record shows the report name, and the date and time it was printed. For example:

          ID   ReportName     PrintDate
          --------------------------------------
           1   rptCustomers   8/13/96 3:25:11 PM
    
    

REFERENCES

For more information about Activate and Deactivate events, search the Help Index for "Activate," or ask the Microsoft Access 97 Office Assistant.

For more information about Print events, search the Help Index for "Print Event," or ask the Microsoft Access 97 Office Assistant.


Additional query words: evaluate diary
Keywords : kbusage RptEvent
Version : 2.0 7.0 97
Platform : WINDOWS
Hardware : X86
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: April 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.