Stephen:
The following does it by setting the Filter property of a report to restrict
it by LastName in its Open event procedure, but it could be equally well use
a query with parameters as the report's RecirdSource:
The Open event procedure for each report goes a follows:
Private Sub Report_Open(Cancel As Integer)
Const FORMNOTOPEN = 2450
Dim frm As Form
On Error Resume Next
Set frm = Forms!frmnamedlg
If Err = FORMNOTOPEN Then
DoCmd.OpenForm "frmNameDlg", OpenArgs:=Me.Name
Cancel = True
Else
If Err <> 0 Then
' unknown error
MsgBox Err.Description, vbExclamation, "Error"
Else
If Not IsNull(frm.txtLastName) Then
Me.Filter = "LastName= """ & frm.txtLastName & """"
Me.FilterOn = True
Else
Me.FilterOn = False
End If
DoCmd.Close acForm, frm.Name
End If
End If
End Sub
The click event of a button on frmNameDlg to open the report goes like this:
Private Sub cmdOpenReport_Click()
Const REPORTCANCELLED = 2501
On Error Resume Next
DoCmd.OpenReport Me.OpenArgs, acViewPreview
Select Case Err.Number
Case 0
' no error so do nothing
Case REPORTCANCELLED
' anticipated error so do nothing
Case Else
'unknown error so inform user
MsgBox Err.Description
End Select
End Sub
To open the report in the application the OpenReport method is called from
somewhere, being sure to hande Err.Number 2501 as above. At this stage
frmNameDlg is not open. This raises an error in the report's Open event
procedure which causes the opening of the report to be cancelled and
frmNameDlg to be opened, with name of the report passed into its OpenArgs
property. When the button on the form is clicked the correct report is
opened by means of the form's OpenArgs property.
You could make the form a little more elaborate by hiding/showing controls
in it depending on the value of its OpenArgs property. That way you can have
different combinations of controls for different reports as appropriate.
Ken Sheridan
Stafford, England