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).
This article describes how you can create a table of contents or an index for areport.
NOTE: This article describes a technique that is demonstrated in the samplefile, 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
Microsoft Access does not have a table-of-contents feature or an indexfeature for reports. However, you can use a table to store descriptionsand page numbers, and then create a report that is based on that table to use as aTable 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 ExplicitDim db As DAO.DatabaseDim TocTable As DAO.RecordsetDim 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 FunctionFunction 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 IfEnd FunctionFunction UpdatePageNumber() intPageCounter = intPageCounter + 1End Function
Open the Products by Category report in Design view, and then set the OnOpen property of the report as follows:
Select the CategoryName header, and then set the OnPrint property of the header as follows:
Select the Page Footer, and then set the OnPrint property of the page footer as follows:
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 thereport to make sure that the Print event is triggered for all records.
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.