Code Samples for Businesses, Schools & Developers

First Published 11 July 2023


NOTE: This is a companion to my earlier article:Is a Form Open as a Subform?

Sometimes it is useful to know using code whether a report has been opened 'standalone' or as a subreport

This is easily done by checking for the report's parent property
If its a subreport, it has a parent (the main report). If not, its a standalone report

To check this for a single report, place this code in the code module for your report:

CODE:

Private Function IsSubreport() As Boolean

'Returns True for a subreport, otherwise false

Dim bHasParent As Boolean

On Error GoTo Err_Handler
      ' If opened not as a subreport, accessing the Parent property raises an error:
      bHasParent=Not (Me.Parent Is Nothing)
      IsSubreport=True
      Exit Function

Err_Handler:
      IsSubreport=False

End Function


You then use code in the Report_Open event as follows:

Private Sub Report_Open(Cancel As Integer)

      If Not IsSubreport Then
            'code here to do something
      Else
            'other code here to do something else (if needed)
      End If

End Sub





If you need to do this for several reports/subreports in your application, then a more generic solution is advisable.

Place the following code in a standard module

CODE:

Public Function IsSubreport(rpt As Access.Report) As Boolean

'---------------------------------------------------------------------------------------
' Procedure:      IsSubreport
' DateTime:       11/09/2014
' Author:            Colin Riddington
' Purpose:          Checks if the specified report is opened as a subreport
'---------------------------------------------------------------------------------------

Dim bHasParent As Boolean

On Error GoTo Err_Handler

      ' If opened not as a subreport, accessing the Parent property raises an error:
      bHasParent = Not (rpt.Parent Is Nothing)
      IsSubreport=True
      Exit Function

Err_Handler:
      IsSubreport=False

End Function


You then use code in the Report_Open event as follows:

Private Sub Report_Open(Cancel As Integer)

      If Not IsSubreport(Me) Then
            'code here to do something
      Else
            'other code here to do something else (if needed)
      End If

End Sub



NOTE:
Almost identical code can be used to check whether a form is being used as a subform.
For more details, see my article Is a Form Open as a Subform?



Feedback

Please use the contact form below to let me know whether you found this article interesting/useful or if you have any questions/comments.

Please also consider making a donation towards the costs of maintaining this website. Thank you



Colin Riddington           Mendip Data Systems                 Last Updated 11 July 2023



Return to Code Samples Page




Return to Top