Code Samples for Businesses, Schools & Developers

First Published 23 Oct 2022                  Last Updated 11 July 2023


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

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

Place this code in the code module for your form:

CODE:

Private Function IsSubform() As Boolean

'Returns True for a subform, otherwise false

Dim bHasParent As Boolean

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

Err_Handler:
      IsSubform=False

End Function


You then use code in the Form_Load event as follows:

Private Sub Form_Load()

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

End Sub


For example, I often use that code as a check in automatic form resizing to resize when opened as a form but not when opened as a subform

Private Sub Form_Load()

      If Not IsSubform Then
            ReSizeForm Me
            Me.NavigationButtons=True
      End If

End Sub


NOTE:
For more information on automatic form resizing, see my 4-part article: A Tutorial in Automatic Form Resizing



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

Place the following code in a standard module

CODE:

Public Function IsSubform(frm As Access.Form) As Boolean

'---------------------------------------------------------------------------------------
' Procedure:      IsSubform
' DateTime:       11/09/2014
' Author:            Colin Riddington
' Purpose:          Checks if the specified form is opened as a subform
'---------------------------------------------------------------------------------------

Dim bHasParent As Boolean

On Error GoTo Err_Handler

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

Err_Handler:
      IsSubform=False

End Function


You then use code in the Form_Load event as follows:

Private Sub Form_Load()

      If Not IsSubform(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 report is being used as a subreport
For more details, see my article Is a Report Open as a Subreport?



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