Microsoft Knowledge Base Email Alertz

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

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 131588 - Last Review: January 19, 2007 - Revision: 2.3

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

This article was previously published under Q131588

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:
145777  (http://kbalertz.com/Feedback.aspx?kbNumber=145777/EN-US/ ) Microsoft Access Sample Reports Available in Download Center

175072  (http://kbalertz.com/Feedback.aspx?kbNumber=175072/EN-US/ ) Microsoft Access 97 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 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.

APPLIES TO
  • Microsoft Access 1.0 Standard Edition
  • Microsoft Access 1.1 Standard Edition
  • Microsoft Access 2.0 Standard Edition
  • Microsoft Access 95 Standard Edition
  • Microsoft Access 97 Standard Edition
Keywords: 
kbhowto kbprogramming kbusage KB131588
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please