Check whether a form is opened as subform (1 Viewer)

wisekat

Registered User.
Local time
Today, 10:21
Joined
Feb 16, 2017
Messages
18
I have a form that is a part of another form (subform), but sometimes I open it as a standalone form. I need to make some changes in the form when it is opened not as a subform. I tried to find a simple Boolean function that indicates whether a form is opened as a subform, and eventually I wrote the following universal function:

Code:
Private Function IsSubform() As Boolean
   Dim bHasParent As Boolean

   On Error GoTo NotASubform

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

   IsSubform = True
   Exit Function

NotASubform:
   IsSubform = False
End Function

I use it this way:

Code:
Private Sub Form_Load()
   Me.NavigationButtons = Not IsSubform()
End Sub

Perhaps, it will help other developers. If you know a simpler solution, let me know.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 08:21
Joined
Feb 19, 2013
Messages
16,552
no - that's the standard solution.

You can make it universal by putting it into a standard model and passing Me as a parameter

Public Function IsSubform(frm as Form) As Boolean
...
...
bHasParent = Not (frm.Parent Is Nothing)
...
...


then call it

Me.NavigationButtons = Not IsSubform(Me)
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 04:21
Joined
Apr 27, 2015
Messages
6,280
Excellent thread, one that made my life much easier this morning. Much appreciated!
 

MarkK

bit cruncher
Local time
Today, 01:21
Joined
Mar 17, 2004
Messages
8,178
You could simplify it like this...
Code:
Property Get IsSubform As Boolean
On Error Resume Next
    IsSubform = Len(Me.Parent.Name) > 0
End Property
 

Users who are viewing this thread

Top Bottom