I have written a VBA procedure to create album pages for my stamp collection. The pages are generated as reports, though no data from the database is used in the report; instead the VBA code uses specifications stored in the database to draw rectangles, lines, text boxes etc. It also adds a couple of lines to event procedures in the report to maximize and restore on open/close of the report. All that works great - the report looks just as it should, and does include the little bits of VBA code. Here is the (very much edited) procedure that produces the report:
Code:
Sub CreatePage(Optional PageNumber As Integer = 1)
Dim rpt As Report, Report_Name As String
Dim db As Database, rst1 As Recordset, rst2 As Recordset, mdl As Module
Dim SQL As String, Part As String, Checkpoint As Integer
Dim Header1 As String, Header2 As String, SEPARATE As Boolean, SEPARATION As Single
Dim Left As Single, Top As Single, Width As Single, Height As Single, Numbers As Variant
Dim RowTop As Single, Frames As Integer, TopOffset As Single, J As Integer, Rowbase As Single
Dim COMMENT1 As String, COMMENT2 As String, Inside() As String, ExtraVertical As Single, TitleWidth As Single
Dim TITLELEFT1 As Single, TITLELEFT2 As Single, TITLERIGHT As Single, lngReturn As Long
Const MAXHEIGHT As Single = 10.2 * 25.4
On Error GoTo PageError
Set db = CurrentDb
'
' Create the new report, based on the page template
'
'Set rpt = CreateReport(, "Page Template")
Set rpt = CreateReport
DoCmd.Minimize
Report_Name = rpt.Name
'--------------------------------------
' Many lines snipped, which I know work
'--------------------------------------
'
' Add VBA to open and close events
'
Set mdl = rpt.Module
lngReturn = mdl.CreateEventProc("Open", "Report")
mdl.InsertLines lngReturn + 1, "docmd.maximize"
lngReturn = mdl.CreateEventProc("Close", "Report")
mdl.InsertLines lngReturn + 1, "popform" & vbCrLf & "docmd.restore"
DoCmd.Close acModule, rpt.Module
DoCmd.Close acReport, Report_Name, acSaveYes
DoCmd.Rename "Album_Page", acReport, Report_Name
Exit Sub
PageError:
MsgBox "Error: " & Err.Description & vbCrLf & vbCrLf & "when printing " & Part & vbCrLf & "Check point = " & Checkpoint
DoCmd.Save , "Stamp_Page" ' Save report defined to this point
DoCmd.Close
End Sub
Now the problem. The procedure is called from a button on the form to enter the specifications. It runs fine, and creates and saves the new report, but when it ends, it leaves behind the open and visible VBA code window, which may or may not have a code module open in it, depending on how I last used it. I think it is being opened by the bit of code that adds the VBA to the report (when I comment that code out, the problem goes away). But how do I close it (other than manually) in the procedure?
Any suggestions would be much appreciated.
Thanks
John