ACC: How to Create a Table of Contents or Index for a Report

ID: Q131588

The information in this article applies to:

SUMMARY

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

This article shows you how to create a table of contents or an index for a report.

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.

NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access versions 1.x and 2.0. For more information about Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x or the "Building Applications" manual in Microsoft Access version 2.0

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

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

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

   earlier).

2. Create the following table and 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 on your report that you will use as a table of content heading.

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

      In Microsoft Access 2.0, 7.0, and 97:

         Option Explicit
         Dim db As Database
         Dim toctable As Recordset

      In Microsoft Access 1.x:

         Option Explicit
         Dim db As Database
         Dim toctable As Table

4. Create the following procedure.

   NOTE: In the following sample code, an underscore (_) at the end of a
   line is used as a line-continuation character. Remove the underscore
   from the end of the line when re-creating this code in Access Basic.

      In Microsoft Access 2.0, 7.0, and 97:

         Function InitToc ()
            ' Called from the OnOpen property of the report.
            ' Opens the database and the table for the report.
            Dim qd As QueryDef
            Set db = CurrentDb()
            ' 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", _
               DB_Open_table)
            toctable.index = "Description"
         End Function

      In Microsoft Access 1.x:

         Function InitToc ()
            ' Called from the OnOpen property of the report.
            ' Opens the database and the table for the report.
            Dim qd As QueryDef
            Set db = CurrentDB()
            ' Delete all previous entries in the Table of Contents table.
            Set qd = db.CreateQueryDef("Delete TOC Entries", "Delete * _
               From [Table of Contents]")
            qd.Execute
            qd.Close
            ' Open the table.
            Set toctable = db.OpenTable("Table Of Contents")
            toctable.index = "Description"
            Exit Function
         End Function

      In all versions:

         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] = Rpt.page
               toctable.Update
            End If
         End Function

5. Open the Products By Category (or List Of Products By Category in
   version 2.0 or earlier) report in Design view and set the report's
   OnOpen property as follows:

      =InitToc()

6. Select the CategoryNameheader and set the header's OnPrint property as
   follows:

      =UpdateToc([CategoryName],Report)

   NOTE: In version 2.0 or earlier, type a space in Category Name.

   Note that when you preview or print the report, the Table Of Contents
   table is updated. The Table Of Contents table records the page on which
   each new category begins.

   NOTE: If you are previewing the report, page through all the pages
   of the report to ensure that the Print event is triggered for all
   records.

7. Create another report based on the Table Of Contents table to print
   the table of contents.

To print the table of contents, print the Products By Category (or List Of Products By Category in version 2.0 or earlier) report first, and then print the Table Of Contents report.

Additional query words:

Keywords          : kbusage RptOthr 
Version           : 1.0 1.1 2.0 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto

Last Reviewed: November 21, 1998