VBA Access check if Parent form exists
Asked Answered
N

7

5

in MS Acces I'm opening a Dialog Form from another Dialog form.

So formA, opens formB. But they user can potentially open formB as standalone, I would like to avoid having errors in this case.

I thought about checking for and Existing parent for formB.

But when I do it I get still get the Error 2452: The expression you entered has invalid to the Parent property.

I tried:

If Not IsError(Me.Parent) Then
    Me.Parent.cboTraining.Requery
End If

And

If Not IsNull(Me.Parent) Then
    Me.Parent.cboTraining.Requery
End If
Nappy answered 7/9, 2015 at 8:46 Comment(1)
A control has a parent, but a separate form does not. Try IsLoaded msdn.microsoft.com/en-us/library/office/ff194656.aspxPod
P
5

You can test if a form is open using:

If Not CurrentProject.AllForms("someFormName").IsLoaded Then

Alternatively, when you open formB from formA, you could provide an openArg, which is easy to test.

Piecework answered 7/9, 2015 at 11:45 Comment(0)
P
2

This approach is similar to above: https://mcmap.net/q/1947515/-vba-access-check-if-parent-form-exists

Public Function hasParent(F As Object) As Boolean
'Inspired from: https://access-programmers.co.uk/forums/showthread.php?t=293282   @Sep 10th, 2019
   Dim bHasParent As Boolean
   On Error GoTo noParents

   bHasParent = Not (F.Parent Is Nothing)
   hasParent = True
   Exit Function

noParents:
   hasParent = False
End Function

Aimed to be more general, and could be called from both subforms/subreports, as below:

If hasParent(Me) Then
   msgbox "Has Parent :)"
   Me.Parent.text1 = "Yes"
else 
   msgbox "NO PARENTS .. :("
endif
Peignoir answered 11/9, 2019 at 7:58 Comment(0)
S
2

This is my solution.

Public Function CheckParentFormExists(P_Form As Form) As Boolean
On Error GoTo Err_Hendler
    Dim P_ParentForm As String
    P_ParentForm = P_Form.Parent.Name
    CheckParentFormExists = True
    
Exit_1:
    Exit Function
Err_Hendler:
    If Err.Number = 2452 Then
        CheckParentFormExists = False
        GoTo Exit_1
    Else
        Debug.Print Err.Number & Err.Description
    End If
End Function

exemple to call :

if CheckParentFormExists(me) then
    ....
Else

End if
Staub answered 20/3, 2022 at 14:51 Comment(0)
G
1

My sometimes subform is called from multiple forms, so I added a generic function to my ss_utilities module based on the simple idea of catching the error:

Public Function hasParent(ByRef p_form As Form) As Boolean
'credit idea from https://access-programmers.co.uk/forums/showthread.php?t=157642
On Error GoTo hasParent_error
    hasParent = TypeName(p_form.Parent.Name) = "String"
    Exit Function
hasParent_error:
    hasParent = False
End Function

So the original question's code would be:

If ss_utilities.hasParent(Me) Then
    Me.Parent.cboTraining.Requery
End If
Glow answered 2/12, 2018 at 16:58 Comment(0)
G
0

To find any independent open form try this:

Sub test()
Dim i
For i = 0 To Forms.Count - 1
    Debug.Print Forms.Item(i).Name
    If Forms.Item(i).Name Like "formA" Then MsgBox "It's Open"
Next i
End Sub
Genome answered 7/9, 2015 at 11:21 Comment(0)
A
0

Here is another example.

Const conObjStateClosed = 0
Const conDesignView = 0

If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> conObjStateClosed Then
    If Forms(strFormName).CurrentView <> conDesignView Then
        MC_FormIsLoaded = True
    End If
End Ifere
Avowal answered 9/7, 2017 at 18:1 Comment(0)
S
0

My short version, and another handy function:

Public Function HasParent(v As Variant) As Boolean
On Error Resume Next
HasParent = IsSomething(v.Parent)
End Function

Public Function IsSomething(ob As Object) As Boolean
  IsSomething = Not ob Is Nothing
End Function
Sport answered 11/3, 2023 at 21:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.