Check whether a form is opened as subform

wisekat

Registered User.
Local time
Today, 20:57
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.
 
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)
 
Excellent thread, one that made my life much easier this morning. Much appreciated!
 
I need to revive this old thread because I need to determine if a form is a sub-form or not and am having a terrible time calling and using this code:
Code:
Public Function IsSubform(Frm As Form) 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 (Frm.Parent Is Nothing)
   IsSubform = True
   Exit Function
NotASubform:
   IsSubform = False
End Function
I am looping through my forms collection and need to see if any of the form objects are sub-forms:
Code:
Dim obj As Object
For Each obj In CurrentProject.AllForms
    If obj.IsLoaded Then
        'Do Nothing
    Else 'forms NOT loaded OR NOT Subforms
        MsgBox obj.Name
        How do I call IsSubform(Frm As Form) As Boolean       
    End If
Next
If I use:
IsSubform (obj)
I get error message:
1727112398851.png

Obviously, I don't know how to use this Public Function.
Can anyone give me the line of code to determine if a form is a sub-form or not?
Thank you so much in advance.
 
How can you tell if they are subforms, unless the form is open?
A form could be a mainform in one instance and a subform in another?
You could use a naming convention? if a form is only ever a subform?
 
I think I would do it the other way.
Loop the forms collection which contains all open forms
--- Loop all controls in the form
------- If control.controlType = acSubform then get the Name of the source object in the subform control and do something with it.
 
I should have explained what I am doing first, I guess. I am populating a custom RibbonUI combobox with forms that are NOT already open OR are NOT sub-forms. I am able populate the combobox with forms that are not open using .IsLoaded, but am not able to determine which forms have Parents or not.

That is the reason I am attempting determine if a form is a sub-form or not just by using the forms collection. I got the code for this from the first post (2017) and thought it might be what I was looking for, but I can't seem to call the Public Function properly so it will assess the form object to determine if it has a Parent or not.
 
Access will not know if a form has a parent or not unless it is open?
 
Access will not know if a form has a parent or not unless it is open?
I believe you are correct. It probably won't even have a Parent property if it's not open.
 
Here is a link describing some of the things that a .Parent property can reference.


It doesn't mention one case that I recall running into / tripping over once - if the sub-form is in a header or footer section, that section's name might be its parent. It doesn't seem to do that for cases where the form in question has no such sections.
 
1. You can tell what forms are loaded as main forms by looping allforms and isloaded = true
2. You can tell what forms are not loaded as main forms by looping allforms and isloaded = false
3. You can tell what subforms are loaded by looping the forms collection, looping the controls, and see what forms are inside the subform controls
4. You could potentially get subforms that are not loaded by
looping the allforms
opening forms not loaded but hidden or in design view
loop the controls
and see what form is inside the subform control.

The problem with opening closed forms hidden is that code could run in the load event.
The problem with opening in design view is cannot be done is a accde
 
1. You can tell what forms are loaded as main forms by looping allforms and isloaded = true
2. You can tell what forms are not loaded as main forms by looping allforms and isloaded = false
3. You can tell what subforms are loaded by looping the forms collection, looping the controls, and see what forms are inside the subform controls
4. You could potentially get subforms that are not loaded by
looping the allforms
opening forms not loaded but hidden or in design view
loop the controls
and see what form is inside the subform control.

The problem with opening closed forms hidden is that code could run in the load event.
The problem with opening in design view is cannot be done is a accde
I am going to set all the subform Tag properties with a "Subform" comment, then check the property as I loop through them. Maybe that will do it. The problem with populating the custom RibbonUI combobox is that it runs (initializes) when the application opens no matter what, so the routine to populate the combobox runs before anything else happens including loading forms.

As most of the people in this forum know, I never was a programmer and don't consider myself one even now after using ACCESS since 1999. I just like to learn new stuff and have fun doing it.
 
If your subforms will only ever be subforms, then give them a name to identify that.
I name my forms frmxxxxx, and only ever subforms sfrmxxxxx
 
If your subforms will only ever be subforms, then give them a name to identify that.
I name my forms frmxxxxx, and only ever subforms sfrmxxxxx
Yes, that is the best way to identify them.
 
Would it just be simpler to make a small table.

tblForms
-- FormName (frmUserDet)
-- DescriptiveName (User Details Form)
-- FormUse (Main form, Sub form, Both)

Super easy to load your ribbon.
 
Lets say you have a couple thousand forms and subforms and do not want to populate this by hand. Write some simple code to update the table. This gets rid of all the problems.
 

Users who are viewing this thread

Back
Top Bottom