I found multiple report instancing to be a difficult task but even more difficult was supplying each instance with different criteria. Creating multiple instances involves declaring a new instance of the report rather than using the DoCmd.OpenReport method. For example
Code:
Private moReport as Report
Private moReportsCollection as New Collection
Private Sub ShowReport(sReportName As String)
Select Case sReportName
Case "rptReport1"
Set moReport = New Report_rptReport1
Case "rptReport2"
Set moReport = New Report_rptReport2
Case Else
MsgBox "Invalid Report Name"
End Select
moReport.Visible = True
moReportsCollection.Add
End Sub
When you use this method however, you cannot use OpenArgs to supply criteria to the report. My solution was to have each report determine if it was opening without supplied criteria. If criteria was not supplied, the report looks at a Report Wizard public property to verify that a user clicked a Preview button (this prevents the report from being opened from the database window without any criteria). The public property is called Params and is a collection of criteria and other parameters used by the report. Here's the code from one report.
Code:
Option Compare Database
Private msCriteria As String
Private msReportID As String
Private Sub Report_Open(Cancel As Integer)
'This report gets selected criteria from the Report Wizard.
'It cannot be opened directly from the database window.
'The Report Wizard has a public propertiey named Params
'that contains criteria statements and a human readable form of the criteria.
Dim oWizard As Form_frmRnmReportWizard
Dim sPrompt As String
If msCriteria = "" Then
'No criteria is specified. Look to Report Wizard for report criteria.
If CurrentProject.AllForms("frmRnmReportWizard").IsLoaded Then
'Report Wizard is open. Ensure that this report has permission to open.
'Permission is granted if this report is currently selected and user had clicked Preview
Set oWizard = Form_frmRnmReportWizard
If oWizard.OpenPermission(Me.Name) = True Then
msCriteria = oWizard.Params("WhereSql")
Me.RecordSource = "SELECT * FROM qryRnmTrapGroups_XField " & _
"WHERE " & msCriteria
'Add taskbar button for popup form
SetWindowLong Me.hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW
Call SetImage("imgTitlePage", "TitlePageImage")
Call SetImage("imgPageHeader", "PageHeaderImage")
msReportID = DateDiff("s", 0, Now)
Me.txtReportID = msReportID
Me.Caption = "RNM Traps Daily Totals - " & msReportID
Me.txtCriteriaText = "Selected criteria for report ID " & msReportID & ":" & _
vbCrLf & oWizard.Params("WhereText")
'Once this report opens from the Report Wizard, the Open event is triggered.
'The Report Wizard uses this event to set the OpenPermission property to False
Else
sPrompt = "Use the Report Wizard to open reports"
MsgBox sPrompt
Cancel = True
End If
Else
'Report Wizard is not open. Open the Wizard and close this report.
Cancel = True
DoCmd.OpenForm FormName:="frmRnmReportWizard", OpenArgs:=Me.Name
End If
End If
End Sub
There's more to this such as the different criteria collection forms and the way that the Report Wizard calls the BuildCriteria sub of each. I can post more details if you want to explore this route.