ACC2000: How to Create a Table of Contents or Index for a Report
ID: Q210269
|
The information in this article applies to:
Moderate: Requires basic macro, coding, and interoperability skills.
This article applies only to a Microsoft Access database (.mdb).
SUMMARY
This article shows you how to create a table of contents or an index for a
report.
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. You may want to back up the Northwind.mdb file
and perform these steps on a copy of the database.
- Open the sample database Northwind.mdb.
- 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.
- Create a module and type the following lines in the Declarations section.
Option Explicit
Dim db As DAO.Database
Dim TocTable As DAO.Recordset
- 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()
'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] = Rpt.Page
TocTable.Update
End If
End Function
- Open the Products by Category report in Design view, and set the OnOpen property of the report as follows:
=InitToc()
- Select the CategoryName header, and set the OnPrint property of the header as follows:
=UpdateToc([CategoryName],Report)
NOTE: 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.
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.
- 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 report first, and then print the Table Of Contents report.
Additional query words:
Keywords : kbusage kbdta AccCon RptOthr
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto