I have a subroutine that generates a table of contents. The subroutine works perfectly. I execute the code, it opens a report used to develop an index of pages saved in a table of contents table, and then pages down through the report to enter each record's page into the table of contents table. Once executed, it takes about five seconds for the report to be opened and paged all the way through.
Here is where my troubles start. I would like for the code to close the report after it has generated the table – so I have added a close report snippet to the code. The problem I have now is that the code somehow runs all the way through the page down loop and then closes the report before (I am guessing) the processor can complete the actual process. Thus the table only reflects a page or two of contents before the report is close.
So, is there a way to pause the code while the processor is catching up?
Private Sub B_TOC_Click()
On Error GoTo Err_B_TOC_Click
Dim PG As Integer
Dim stDocName As String
' SET REPORT NAME
stDocName = "R_TOC_CBL_SHT"
' OPEN REPORT
DoCmd.OpenReport stDocName, acPreview
' DETERMINE TOTAL NUMBER OF REPORT PAGES
Forms!MAIN!C_Ttl = Reports!R_TOC_CBL_SHT.Pages
' GO TO PAGE #2
' START PAGE COUNT DOWN
PG = Reports!R_TOC_CBL_SHT.Pages - 1
Do While PG > 0
' NEXT PAGE OF REPORT
' PAGE IS DONE, SUBTRACT FROM TOTAL PAGES
PG = PG - 1
' LOOPS "DO" STATEMENT
DoCmd.Close acReport, stDocName