SubReport-TOC/Html-linked TOC

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!
Who is Participating?
paaskyConnect With a Mentor Commented:
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,
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.
Forgot to mention that I there's always page break before "Article section" - it will force next article always start from new page.
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

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.

mjustmanAuthor Commented:
Adjusted points from 100 to 150
mjustmanAuthor Commented:

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

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

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>

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.