Function if form isloaded as form or as a subform

MsAccessNL

Member
Local time
Today, 16:07
Joined
Aug 27, 2022
Messages
222
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:
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:
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
 
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.
 
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:
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.
 
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
 
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)
 
@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.
 
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.
 
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:
Going back to post #1, you wrote:


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
Wow, your code is so much shorter then mine. This is Exactly what I was asking for in my post . Thnx.
 
a (short) function that checkes if a form isloaded as a form and also if it's loaded as a subform.
Not sure what you really mean. I interpret that to simply mean you want code to determine if a form is loaded as a subform not if it is loaded both as a main form and also as a subform.

Isladogs showed how to show if a form is loaded as a main form.
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

To see if a form is loaded as a subform
Code:
Public Function IsSubFormLoaded(ByVal strFormName As String) As Boolean
  Dim sFrm As Access.Form
  Dim ctrl As Access.Control
  For Each sFrm In Forms
    For Each ctrl In sFrm.Controls
      If ctrl.ControlType = acSubform Then
        If ctrl.Form.Name = strFormName Then
          IsSubFormLoaded = True
          Exit Function
        End If
      End If
    Next ctrl
  Next sFrm
End Function

If you really want to know if it is loaded as either a main form or a subform, then wrap those functions
Code:
Public Function isFormOrSubFormLoaded(ByVal strFormName As String) As Boolean
  If IsFormLoaded(strFormName) Or IsSubFormLoaded(strFormName) Then isFormOrSubFormLoaded = True
End Function
 
solved?
but the title is asking if the form is "Loaded" (meaning not a subform) or a subform.
i have a sample db, but much longer than your code.
it will test (on my case, Form1 (see test sub on module1)) the form's status.

possible results:

"Not exists" (if the specified form name does not exists in the db).
"Not loaded" (the form exists but not Open (not in design view), or not a subform).
"Loaded" (if the form is open (normal view) and not a subform)
"Subform" (when the form is a subform)
"SubSubform (prefix "Sub" is repeating, when it is x many times deep embed as subform)

i don't know if anybody can find this useful? :unsure:
 

Attachments

Perhaps I'm missing the point but discussion in this thread seems very convoluted

A form object can be opened as a form or as a subform (or it can be a subreport)
However, for a form object to be detected as e.g. a subform, it must be loaded (open).

Is there some point of significance that I'm overlooking?
 
I suspect a subform isn't loaded in the way a normal form is, as it is an instance of the form contained in a subform/subreport control.

However if the so-called "subform" is opened independently it is no longer a subform, it's just an ordinary form and can be detected.

To reiterate the op's question in the hope of clarifying the situation, he found some code which would tell him if a form was open. He discovered that it didn't identify subforms as being open.

He suggested that finding the subforms parent would indicate if it was subform.

Another approach would be to find all of the subform/subreport controls and see what the source object name is. Check that against the collection of forms.
Code:
Public Function IsLoaded(Search As String, Optional Scan As Object) As Boolean
    Dim i As Object
    Dim First As Boolean
    If Scan Is Nothing Then First = True: Set Scan = Application.Forms
    For Each i In Scan
        If Not First Then
            If i.ControlType = 112 Then
                If Search = i(0).Parent.Name Then IsLoaded = True: Exit Function
                IsLoaded = IsLoaded(Search, i(0).Parent)
                If IsLoaded Then Exit Function
            End If
        Else
            If Search = i.Name Then IsLoaded = True: Exit Function
            IsLoaded = IsLoaded(Search, i)
            If IsLoaded Then Exit Function
        End If
    Next
End Function
 
@MsAccessNL 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.
In the whole discussion and in the presented solutions it has not yet been considered that one can work with several instances of the same form at the same time - and that is anything but confusing if I already have a good and universally usable form.
There can be different design options for these different instances:

- Multiple instances as main form (input form versus data display form)

- Use as a main form and as a subform of another form

- Use as a multiple subform with different data displays in the same main form

If there are several instances, addressing the form by name is not an option. For targeted work, addressing by reference is necessary. It's also important to note that a form is a class and its instance must be able to determine for itself what it is, where it resides, and what it's called as a control, to perform specific actions or not to execute.

With that in mind, I've added something to the DB file of @arnelgp to allow for easy testing.
The simple function:
Code:
' sample call
Private Sub Form_Open(Cancel As Integer)
    Dim SourceObjectName As String
    Dim ParentFormName As String
    Dim UFoControlName As String

    Debug.Print
    Debug.Print "Is SubForm: " & IsSubform(Me, SourceObjectName, ParentFormName, UFoControlName)
    Debug.Print "SourceObjectName: " & SourceObjectName, _
                "ParentFormName: " & ParentFormName, _
                "UFoControlName: " & UFoControlName
End Sub
Code:
Public Function IsSubform(AnyForm As Access.Form, _
                          Optional ByRef SourceObjectName As String, _
                          Optional ByRef ParentFormName As String, _
                          Optional ByRef UFoControlName As String) As Boolean
    SourceObjectName = AnyForm.Name
    If SysCmd(acSysCmdGetObjectState, acForm, AnyForm.Name) = 0 Then
        IsSubform = True
        ParentFormName = AnyForm.Parent.Name
        UFoControlName = SubformControlName(AnyForm)
    Else
        IsSubform = False
    End If
End Function

Public Function SubformControlName(SubformOBJ As Access.Form) As String
    Dim ctl As Access.Control
   
    For Each ctl In SubformOBJ.Parent
        If ctl.ControlType = acSubform Then
            If ctl.SourceObject = SubformOBJ.Name Then
                If ctl.Form.Hwnd = SubformOBJ.Hwnd Then
                    SubformControlName = ctl.Name
                    Exit For
                End If
            End If
        End If
    Next
End Function
 

Attachments

Users who are viewing this thread

Back
Top Bottom