Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


SubReport-TOC/Html-linked TOC

Posted on 2000-05-07
Medium Priority
Last Modified: 2012-08-14
Two questions with a common thread:
I have a simple report (done) that needs a simple Table of Contents (done, and in place as a subreport, but I've disassociated it for now, partly because I couldn't force a page break at its end...make that an added value item!).  The  TOC needs to pick up the page numbers from the generated report pages (per item of course).  I'm guessing the secret is in some code in the Print event of the report? ...  but do I need to bury any hooks in the underlying tables?

Then: I got "creative" and tried out the export-to-html function. Overall, not bad. BUT I need to build (this time, two levels of) TOC pages hyperlinked to the detail below.  Can I make Access do the work?  It's generating the pages and knows their titles, tho I could probably guess most of string (up to the sequence number).  What function could I put where to make this happen?

many thanks as usual!

Marilyn Justman

Ask for more points if it's trickier than I think!
Question by:mjustman
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
LVL 10

Accepted Solution

paasky earned 450 total points
ID: 2796012
Hello Marilyn,

Here's some suggestions to your first question. The second seems much harder to solve and might require quite serious coding depending how complex is your report because it might not be solved using export-html-function but a customer function that would parse data to html pages.

In my sample example I've got a table "Articles" which has the following fields:

Article_ID   Autonumber (PK)
Topic        Text 200
Article      Memo
PageNum      Integer

After I added some test data into table I created a main report which has the following visible sections: Report Header, Page Header, Article Header, Detail, Article Footer and Page Footer.

In the report header there's cover page information and I also added there two pagebreak controls (later I added TOC subform there).

After this was done, I added the following code to the report:

' declare variable which is used to determine if article
' continues to next pages (when True, show label "(Continued...)"
Dim Continued As Boolean

Private Sub ArticleFooter_Format(Cancel As Integer, FormatCount As Integer)
    ' new article starts
    Continued = False
End Sub

Private Sub ArticleHeader_Format(Cancel As Integer, FormatCount As Integer)
Dim Sql As String

    Continued = True
    ' update page numbers that are shown in TOC
    Sql = " UPDATE Articles SET PageNum = " & CStr(Me.Page) & _
          " WHERE Article_ID = " & CStr(Me![txtArticle_ID])
    CurrentDb.Execute Sql

End Sub

Private Sub PageHeader_Format(Cancel As Integer, FormatCount As Integer)
    Me![lblContinued].Visible = Continued
End Sub

Then I made the subform which data source is same as main report but has only detail section which shows topic and page number so it tooks like this:

Topic I             3
Topic II           15          

After I have added this subreport to my main report's Report Header section this was almost done. Now only I need to add a code that runs the report twice: first run defines the correct page numbers to TOC and the second time its shown correctly.

In my print button there is the following code:

Private Sub B_Print_Click()

    On Error Resume Next

    ' prepare report - this is not shown to user
    Application.Echo False
    DoCmd.OpenReport "Article", acViewPreview
    DoCmd.Close acReport, "Article"
    Application.Echo True
    ' now show the report
    DoCmd.OpenReport "Article", acViewPreview
End Sub

When I press the print button I get the report which has cover page, the next page is TOC (with page numbers) and articles starts from page 3.

I can send you my sample db if you like.

Hope this helps,
LVL 10

Expert Comment

ID: 2796023
If you don't want to add PageNum field to your source tables, you can easily do a (temporary) worktable which should contain grouping and page number fields and instead of updating it, you need to fill it on the first run and then join its fields to report tables with a query.
LVL 10

Expert Comment

ID: 2796045
Forgot to mention that I there's always page break before "Article section" - it will force next article always start from new page.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 10

Expert Comment

ID: 2796096
After thinking more of this (I've been suffering flu few days now so my mind doesn't work as well as it could) I might have an idea how to create html TOC that has hyperlink links to articles:

You need a code that creates only TOC html page. It could work like this (here's pseudo-code):

1. Open file for output
2. Open Recordset "Articles"
3. Print the html page header tags to file
4. Loop all articles and write the following information to file: <A href="Article_Page" & Cstr(rs![pagenum] &) ".html>" & rs![Topic] & "</A><P>"
5. Write html footer tags
6. Close file and recordset

If you need any assistance with these, please let me know.


Author Comment

ID: 2796532
Adjusted points from 100 to 150

Author Comment

ID: 2796534

Many thanks!

I'm accepting your first "Comment" as answer, but anyone who reads this should read the following comments as well.

I haven't tried it yet--that is a volunteer project and I've got a "real" one cooking today.  Will try to comment after I've tried it.

Sure: I'd LOVE to see your model. If it'll zip down to 2 meg or less, please just email pkg to
Otherwise, send correspondence there and we'll figure out a better way.

Marilyn Justman
LVL 10

Expert Comment

ID: 2796955

Here's the code of form which saves report as HTML and creates TOC in HTML format:

' define here the html file path and name
' when using output-method, the name is usually
' <reportname>Page<pagenumber>.html
' the code will add <pagenumber>.html
Const PAGENAME = "Article"

Private Sub B_BuildHTML_Click()
Dim Outputfile As String
Dim rst As Recordset
Dim html As Recordset
Dim f As Integer

    On Error GoTo Build_Error

    Outputfile = InputBox("Enter HTML TOC file name:", "TOC file", "c:\TOC.html")
    If Outputfile = "" Then Exit Sub
    Set rst = CurrentDb.OpenRecordset("Articles")
    ' no articles?
    If (rst.BOF And rst.EOF) Then
        Set rst = Nothing
        Exit Sub
    End If
    f = FreeFile()
    Open Outputfile For Output As f
    ' write html header to file
    Set html = CurrentDb.OpenRecordset("select [data] from [HTML] where [SectionCode]=1 order by [RowID]")
    While Not html.EOF
        Print #f, html![Data]
    ' write toc and link to file
    With rst
        While Not .EOF
            Print #f, "<A HREF=" & Chr(34) & PAGENAME & ![PageNum] & ".html" & Chr(34) & ">";
            Print #f, ![Topic];
            Print #f, "</A><P>"
    End With
    Set rst = Nothing
    ' write html footer to file
    Set html = CurrentDb.OpenRecordset("select [data] from [HTML] where [SectionCode]=2 order by [RowID]")
    While Not html.EOF
        Print #f, html![Data]
    Set html = Nothing
    Close f
    MsgBox "TOC succesfully written to file " & Outputfile

    Exit Sub
    MsgBox Err.Description
    Resume Build_Exit
End Sub

Private Sub B_SaveAsHtml_Click()
    DoCmd.OutputTo acOutputReport, "Article", acFormatHTML
End Sub

LVL 10

Expert Comment

ID: 2796961
Table HTML contains the following data:

RowID      Data      SectionCode
1      <HTML>      1
2      <HEAD>      1
3      <TITLE>Table Of Contents</TITLE>      1
4      </HEAD>      1
5      <BODY>      1
6      <H2>Table Of Contents</H2>      1
100      </BODY>      2
101      </HTML>      2

SectionCode defines which part of HTML document the data is, 1 = header, 2 = footer.

And this is an example of TOC.html created with code:

<TITLE>Table Of Contents</TITLE>
<H2>Table Of Contents</H2>
<A HREF="ArticlePage3.html">A Technical Introduction to XML (By Norman Walsh)</A><P>
<A HREF="ArticlePage13.html">Beyond HTML: XML and Automated Web Processing</A><P>
<A HREF="ArticlePage19.html">Exploring XML-RPC (Byte Magazine)</A><P>
<A HREF="ArticlePage20.html">Going from HTML to XML</A><P>
<A HREF="ArticlePage24.html">Introduction to XML (By Lars Marius Garshol)</A><P>


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question