
Two Virtually Identical Modules, One Works, One doesn't
I am using Access 7.0 and I would like to generate a table of contents and
an index for a report. I've downloaded the sample reports database from
the MS Web site and copied the table of contents module into the database
that generates the report. The code is as follows:
Option Compare Database
Option Explicit
Dim db As DATABASE
Dim toctable As Recordset
Function InitToc()
' Called from the OnOpen property of the report.
' Opens the database and the table for the report.
Set db = CurrentDb()
db.Execute "DELETE * FROM tblTableOfContents;"
'
' Open the table.
Set toctable = db.OpenRecordset("tblTableOfContents",
dbOpenTable)
toctable.Index = "Description"
End Function
Function UpdateToc(tocentry As String, Rpt As Report)
' Called from the OnPrint property of the section containing
' the Table Of Contents entry. Updates the Table Of Contents
' entry.
toctable.Seek "=", tocentry
If toctable.NoMatch Then
toctable.AddNew
toctable!Description = tocentry
toctable![page number] = Rpt.Page
toctable.UPDATE
End If
End Function
As per the sample database, I created a table to receive the generated
records and set the functions to be called from the appropriate On Format
locations in the report. When I run the report, I receive an error stating
that the On Format expression has a "type mismatch", but Access gives no
indication of what field is involved. As far as I can tell, all the
corresponding fields match and I can't see why I'm getting the error.
To create the report index, I created a copy of the module and the
receiving table and changed the "toctable" references to "indextable",
"tocentry" to "indexentry" and "tblTableOfContents" to "tblIndex". I call
it from an exact copy of the original report and it works flawlessly. I
modified it to allow duplicates in the "description" index and still had no
problems.
I decided to strip out the changes and start from scratch. I recreated
everything and, as before, the Index module works and the Table of Contents
module doesn't. Only this time, the Index module creates two records at a
time when it should produce only one and I can't find any cause for the
duplication.
I can't tell if I'm missing something obvious, something not well
documented (that the two modules are conflicting, some keyword violation,
some initialization requirement or something else) or if this a bug or
limitation in Access itself. Any suggestions would be greatly appreciated.