Function if form isloaded as form or as a subform (1 Viewer)

MsAccessNL

Member
Local time
Today, 15:30
Joined
Aug 27, 2022
Messages
184
I was looking for a (short) function that checkes if a form isloaded as a form and also if it's loaded as a subform. I have found some posts about this subject. The currentproject.allforms...isloaded seems not to work when the form is opened as a subform so you have to check this with another function (probably if a Parent form is present or loop all controls). I came up with a short function that can do both (i think). I only tested it shortly. I am looking for info, if this is a possible working solution.

One note: everybody who wants to reply about my bad English, or is anoyed by my post in any other way, please don't reply, just skip the post. All other people who are willingly to share there knowledge (which is probably much greater then mine) in a positive way, please reply. I use Access 2010

Code:
Public Function IsOpen(frm As Form) As Boolean
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' This code is made by Daniel Sanders MsAccess.nl 23-okt-2022
    ' Call IsOpen(Form_FormTest)
     ‘ FormTest needs to have an [event procedure]
    ' True if Form is open (also in design
view) or as Subform
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    If frm.Visible = False Then 'this probably will open F_Public as hidden so you have to close again
        DoCmd.Close acForm, frm.Name ' gives no error as F_Public is not open
        IsOpen = False
    Else
        IsOpen = True
    End If

End Function
 
Last edited:

cheekybuddha

AWF VIP
Local time
Today, 14:30
Joined
Jul 21, 2014
Messages
2,280
Hi,

How do you use this?

Since you are passing a form object as parameter, then the form must be open, no? Even if it is a subform?

I'm guessing you would call it using something like:
Code:
Dim blOpen As Boolean

blOpen = IsOpen(Me)

This doesn't seem very useful!

If you are just trying to find out whether a form is opened stand-alone or as a subform you can just check its .Parent property. If it is opened stand-alone then it will not have a .Parent property and you will get an error. So you can leverage like:
Code:
Function IsOpenedAsSubform(frm As Form) As Boolean
On Error Resume Next

  IsOpenedAsSubform = Not frm.Parent Is Nothing

End If
 
Last edited:

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 14:30
Joined
Jul 9, 2003
Messages
16,282
The currentproject.allforms...isloaded seems not to work when the form is opened as a subform
I suspect you are right. I would imagine that the subform is actually an instance of the form, and hence not being shown as a loaded form.

Your guess that you would need to check to see if the subform has a parent is correct, but the parent of a subform is not another form, it is a subform/subreport control.

In fact what you referred to as a subform is nothing more than a normal form housed within a subform/subreport control.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 14:30
Joined
Jul 9, 2003
Messages
16,282
Your observation piqued my interest because I have spent many hours (if not years!) creating a class module to manage opening a pop-up form and return data back into the calling form.

Early versions of my class module worked if calling the Pop-Up form from a main form, but did not work if I loaded the popup from a subform. To correct this problem I wrote a routine which iterated up through the controls from the active control (the control pressed) to discover if the control was on a subform.

The code that does this iterating up through the controls might be of interest to you. You will find it on my website on the link provided below:-

 
Last edited:

MsAccessNL

Member
Local time
Today, 15:30
Joined
Aug 27, 2022
Messages
184
Hi,

How do you use this?

Since you are passing a form object as parameter, then the form must be open, no? Even if it is a subform?

I'm guessing you would call it using something like:
Code:
Dim blOpen As Boolean

blOpen = IsOpen(Me)

This doesn't seem very useful!

If you are just trying to find out whether a form is opened stand-alone or as a subform you can just check its .Parent property. If it is opened stad-alone then it will not have a .Parent property and you will get an error. So you can leverage like:
Code:
Function IsOpenedAsSubform(frm As Form) As Boolean
On Error Resume Next

  IsOpenedAsSubform = Not frm.Parent Is Nothing

End If
Try :IsOpen(Form_F_Test) and see what happens, do not use Forms!F_Test
 

cheekybuddha

AWF VIP
Local time
Today, 14:30
Joined
Jul 21, 2014
Messages
2,280
Try :IsOpen(Form_F_Test) and see what happens, do not use Forms!F_Test
Yes, but what's the point?

And, as was shown in your other thread, using form classes directly without good reason often causes more problems than it solves.
 

MsAccessNL

Member
Local time
Today, 15:30
Joined
Aug 27, 2022
Messages
184
Your observation piqued my interest because I have spent many hours (if not years!) creating a class module to manage opening a pop-up form and return data back into the calling form.

Early versions of my class module worked if calling the Pop-Up form from a main form, but did not work if I loaded the popup from a subform. To correct this problem I wrote a routine which iterated up through the controls from the the active control (the control pressed) to discover if the control was on a subform.

The code that does this iterating up through the controls might be of interest to you. You will find it on my website on the link provided below:-

Xxx
 
Last edited:

MsAccessNL

Member
Local time
Today, 15:30
Joined
Aug 27, 2022
Messages
184
This is really a strange forum, i think these replies are more about ego, then about the content. Thank you very much of saying to me that the code is useless. An explanation about what problems a could encounter would be welcome, that’s the purpose of my post as i pointed out in the beginning.
 

cheekybuddha

AWF VIP
Local time
Today, 14:30
Joined
Jul 21, 2014
Messages
2,280
Forgive me - I am not trying to put you down.

I am just trying to understand when and where this code will be useful, and just asked for an example of why you would want to use it. I shared my guesses, but you didn't confirm or deny whether I was on the right track or way off base.

Otherwise, I just wanted to point out that using this code (using form classes directly without assigning them to an object variable) can have pitfalls
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 14:30
Joined
Jul 9, 2003
Messages
16,282
assigning them to an object variable) can have pitfalls

Taking this comment slightly out of context I removed the "without".... I recall that as I iterated through the parents I assigned them to a control variable. This worked fine until it got to a form.

You cannot assign a form to a control variable. This had me scratching my head for quite some days I reckon!

The solution was to get the parents name and check it against the collection of forms to see if it existed in the forms collection. If it did then it was a form.

I guess that maybe the pitfall Cheekybuddha has encountered.

The lesson I learnt is to Check the object name, which is always "text" thus avoiding issues in assigning.
 

cheekybuddha

AWF VIP
Local time
Today, 14:30
Joined
Jul 21, 2014
Messages
2,280
You cannot assign a form to a control variable
You ought to be able to assign it to an object variable though, no?

(ps, if this is related to the class you posted above, I haven't had a chance to look at it, so please ignore my comment if irrelevant)
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 14:30
Joined
Jul 9, 2003
Messages
16,282
You ought to be able to assign it to an object variable though, no?
Yes, I would think so. Please bare in mind I wrote that code many years ago! I'm sure it would benefit from a good look with fresh eyes.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:30
Joined
Feb 19, 2002
Messages
43,302
@MsAccessNL Most of the people who post questions are novices, even when they are experienced programmers in other environments. You are new and we don't know you so you might want to give us some idea what your level of expertise is so we can better target our responses. Also keep in mind that novices don't always know the most appropriate solution. They write as you have with questions about how to make their code work. Rather than loading their gun with bullets and pointing it at their head, when they ask odd questions such as this, we try to get some idea of the problem they are trying to solve so we can perhaps offer a better solution.

So, given that. If you don't want to share the problem you are trying to solve all we can do is to load your bullets. If what you are trying to do is to unload a form if it is loaded, you have your answer. The code identifies forms that are loaded. If you know what mainform it is used on, you could check for that also. If you have some other problem, then perhaps you can enlighten us.

Generally it is poor practice to have multiple forms open at one time since it confuses the user and causes problems such as you seem to have. When I have to open a second form, I usually open it model so only it can take the focus. That way random forms don't float around. They get closed when they should be closed.
 

MsAccessNL

Member
Local time
Today, 15:30
Joined
Aug 27, 2022
Messages
184
@MsAccessNL Most of the people who post questions are novices, even when they are experienced programmers in other environments. You are new and we don't know you so you might want to give us some idea what your level of expertise is so we can better target our responses. Also keep in mind that novices don't always know the most appropriate solution. They write as you have with questions about how to make their code work. Rather than loading their gun with bullets and pointing it at their head, when they ask odd questions such as this, we try to get some idea of the problem they are trying to solve so we can perhaps offer a better solution.

So, given that. If you don't want to share the problem you are trying to solve all we can do is to load your bullets. If what you are trying to do is to unload a form if it is loaded, you have your answer. The code identifies forms that are loaded. If you know what mainform it is used on, you could check for that also. If you have some other problem, then perhaps you can enlighten us.

Generally it is poor practice to have multiple forms open at one time since it confuses the user and causes problems such as you seem to have. When I have to open a second form, I usually open it model so only it can take the focus. That way random forms don't float around. They get closed when they should be closed.
Probably you mist my docmd.close command. It’s no a real long code,did you take the time to read it well and test it. The thing is that using Form_F_Test” wlll probably open the Form F_Test hidden (if there is no F_Test open already) .Noe you can check the property Form_F_Test.visible = false or true (if the form was already open it will show true otherwise false.

just try it out and don’t use isopen(me) or isopen(Forms!… But isopen(Form_F_Test)

and the purpose of isopen or name te functiom Isloaded, is rhetorical.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:30
Joined
Feb 19, 2002
Messages
43,302
Why would I build an application to test your function? If you solved your "problem" with it, then use it. If you want us to test it for you, upload a database.
 

MsAccessNL

Member
Local time
Today, 15:30
Joined
Aug 27, 2022
Messages
184
Probably you mist my docmd.close command. It’s no a real long code,did you take the time to read it well and test it. The thing is that using Form_F_Test” wlll probably open the Form F_Test hidden (if there is no F_Test open already) .Noe you can check the property Form_F_Test.visible = false or true (if the form was already open it will show true otherwise false.

just try it out and don’t use isopen(me) or isopen(Forms!… But isopen(Form_F_Test)

and the purpose of isopen or name te functiom Isloaded, is rhetorical.
Why would I build an application to test your function? If you solved your "problem" with it, then use it. If you want us to test it for you, upload a database.
Again, very strange conversation. Upload a databse for 3 lines of code? I think the information i have given is sufficient. Read my post again, my question is clear and the instructions are clearly given I I have also stated that you don’t have to reply. I really don’t get the irritation. May be you have a bad day. Lets end this please. I really don’t enjoy this kind of communication.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:30
Joined
Feb 19, 2002
Messages
43,302
I'm not irritated and if you think we are talking about 3 lines of code, you are missing something. It isn't hard to build a few forms with a few controls to test your code but the people who are trying to help you are not getting paid so asking them to build a test harness to test code that you say works but you want conformation is a misuse of our time.

Perhaps you shouldn't be using a form as a both a subform and a main form:)
 

isladogs

MVP / VIP
Local time
Today, 14:30
Joined
Jan 14, 2017
Messages
18,240
Going back to post #1, you wrote:
I was looking for a (short) function that checkes if a form isloaded as a form and also if it's loaded as a subform.

To check if a form is loaded, you can use:
Code:
CurrentProject.AllForms("YourFormName").IsLoaded
True if loaded, otherwise false

Or here is alternative code for this as a function:
Code:
Function IsFormLoaded(ByVal strFormName As String) As Integer
'Returns a 0 if form is not open or a -1 if Open

On Error GoTo Err_Handler

    If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> 0 Then
        If Forms(strFormName).CurrentView <> 0 Then
            IsFormLoaded = True
        End If
    End If
  
Exit_Handler:
    Exit Function

Err_Handler:
    MsgBox "Error " & err & " in IsFormLoaded procedure : " & Err.Description
    Resume Exit_Handler
  
End Function

Typical usage: IsFormLoaded("YourFormName")

Code to test whether a form is open as a subform:

Code:
Private Function IsSubform() As Boolean
   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

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

Code:
Private Sub Form_Load()

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

EDIT: I've just noticed the IsSubform code in post #2. Sorry I only skimmed the thread
 
Last edited:

Users who are viewing this thread

Top Bottom