Check if Report being opened as Subreport

  • Thread starter Thread starter Alan Z. Scharf
  • Start date Start date
A

Alan Z. Scharf

I'd like to programmatically check if, when a report is opened, whether it
is being opened as a standalone report or whether it is a subreport, ,
i.e.
whether the report has a parent.

Is there a way to do this?


Thanks.

Alan
 
Here are two ways ...

Private Sub Report_Open(Cancel As Integer)

Dim obj As Object

On Error Resume Next
Set obj = Me.Parent
If Err = 0 Then
MsgBox "I'm a sub-report!", , "First Method"
ElseIf Err = 2452 Then
MsgBox "I'm not a sub-report!", , "First Method"
Else
MsgBox "Hey, something unexpected happened!", , "First Method"
End If
On Error GoTo 0

Dim rpt As Report
Dim boolFound As Boolean

For Each rpt In Reports
If rpt.Name = Me.Name Then
MsgBox "I'm not a sub-report!", , "Second Method"
boolFound = True
Exit For
End If
Next rpt
If boolFound = False Then
MsgBox "I'm a sub-report!", , "Second Method"
End If

End Sub
 
Alan said:
I'd like to programmatically check if, when a report is opened, whether it
is being opened as a standalone report or whether it is a subreport, ,
i.e. whether the report has a parent.


Just refer to the parent property and if you get error 2452,
then it's a main report. If no error, it's a subreport.


Here's an air code example of a function in a standard
module:

Public Function IsSubReport(rpt As Report) As Boolean
On Error GoTo ErrorHandler
IsSubReport = Len(rpt.Parent.Name) > 0
ExitHere:
Exit Sub

ErrorHandler:
Select Case Err.Number
Case 2452
IsSubReport = False
Resume ExitHere
Case Else
MsgBox Err.Number & " - " & Err.Description
Raise Err.Number
End Select
End Function

Then you can check in any report's open event:

If IsSubReport(Me) Then
' I am a subreport
Else
' I am a main report
End If
 
I'd like to programmatically check if, when a report is opened, whether it
is being opened as a standalone report or whether it is a subreport, ,
i.e.
whether the report has a parent.

Is there a way to do this?

Thanks.

Alan

Code the sub-report's Open event:

On Error GoTo Err_Handler
MsgBox "This is a sub-report in " & Me.Parent.Name
Exit_Sub:
Exit Sub
Err_Handler:
MsgBox "Not a sub-report."
Resume Exit_Sub

If there is no parent report it will throw an error which will give
the fact that it is not used as s sub-report.
 
Brendan, Fred, and Marshall;

Thanks very much!

I can use all these methods in different places in my reporting system.

Regards,

Alan
 
On reflection, Alan, I realize that there is a flaw in the second method
that I proposed (the one that loops through the Reports collection). It
relies on the fact that a report that is open as a sub-report is not added
to the Reports collection. But it could return an incorrect result if two
instances of the report were open at the same time, one as a sub-report and
one not. It might be safer to avoid that method, and stick with the
trap-the-error method.
 
Back
Top