Microsoft KB Archive/210269

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

Article ID: 210269

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



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.

 Open the sample database Northwind.mdb.  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.

  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   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   Open the Products by Category report in Design view, and then set the OnOpen property of the report as follows: =InitToc   Select the CategoryName header, and then set the OnPrint property of the header as follows: =UpdateToc([CategoryName],Report) </li>  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.

</li> Create another report that is based on the Table Of Contents table to print the table of contents.</li></ol>

To print the table of contents, print the Products by Category report first, and then print the Table Of Contents report.

Keywords: kbhowto kbprogramming kbusage KB210269

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.