ACC2000: How to Create a Table of Contents or Index for a Report (210269)



The information in this article applies to:

  • Microsoft Access 2000

This article was previously published under Q210269
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies only to a Microsoft Access database (.mdb).

SUMMARY

This article describes how you can create a table of contents or an index for a report.

NOTE: This article describes a technique that is demonstrated in the sample file, RptSmp00.mdb.

For additional information about how to obtain this sample file, click the article number below to view the article in the Microsoft Knowledge Base:

231851 ACC2000: Microsoft Access 2000 Sample Reports Available in Download Center

MORE INFORMATION

Microsoft Access does not have a table-of-contents feature or an index feature for reports. However, you can use a table to store descriptions and page numbers, and then create a report that is based on that table to use as a Table of Contents report. You can use the same method to create an index.

To create a report that generates a table of contents, follow these steps.

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.

  1. Open the sample database Northwind.mdb.
  2. Create the following table, and then save it as Table Of Contents:
       Table: Table Of Contents
       -------------------------------
       Field Name: Description
          Data Type: Text
          Field Size: 15
          Indexed: Yes (No Duplicates)
       Field Name: Page Number
          Data Type: Number
          Field Size: Long Integer
          Indexed: No
    					
    NOTE: Make sure the Description field is the same data type as the field that is on your report that you use as a table-of-contents heading.

  3. Create a module, and then type the following lines in the "Declarations" section:
    Option Explicit
    
    Dim db As DAO.Database
    Dim TocTable As DAO.Recordset
    Dim intPageCounter As Integer
    					
  4. Create the following procedure:
    Function InitToc()
        'Called from the OnOpen property of the report.
        'Opens the database and the table for the report.
        Dim qd As DAO.QueryDef
        
        Set db = CurrentDb()
        
        'Resets the page number back to 1
        intPageCounter = 1
        'Delete all previous entries in Table of Contents table.
        Set qd = db.CreateQueryDef("", "Delete * From [Table of Contents]")
        
        qd.Execute
        qd.Close
        
        'Open the table.
        Set TocTable = db.OpenRecordset("Table Of Contents", dbOpenTable)
        
        TocTable.Index = "Description"
    End Function
    
    Function UpdateToc(TocEntry As String, Rpt As Report)
        'Call from the OnPrint property of the section containing
        'the Table Of Contents Description field.
        'Updates the Table Of Contents table.
        TocTable.Seek "=", TocEntry
    
        If TocTable.NoMatch Then
            TocTable.AddNew
            TocTable!Description = TocEntry
            TocTable![page number] = intPageCounter
            TocTable.Update
        End If
    End Function
    Function UpdatePageNumber()
       intPageCounter = intPageCounter + 1
    End Function
    					
  5. Open the Products by Category report in Design view, and then set the OnOpen property of the report as follows:
    =InitToc()
    					
  6. Select the CategoryName header, and then set the OnPrint property of the header as follows:
    =UpdateToc([CategoryName],Report)
    					
  7. Select the Page Footer, and then set the OnPrint property of the page footer as follows:
    =UpdatePageNumber()
    NOTE: When you preview or you print the report, the Table Of Contents table is updated. The Table Of Contents table records the page on which each new category begins.

    When you preview the report, page through all of the pages of the report to make sure that the Print event is triggered for all records.

  8. Create another report that is based on the Table Of Contents table to print the table of contents.
To print the table of contents, print the Products by Category report first, and then print the Table Of Contents report.

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbhowto kbProgramming kbusage KB210269