VBA Access check if Parent form exists

橙三吉。 提交于 2020-06-14 09:46:45

问题


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

回答1:


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.




回答2:


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



回答3:


This approach is similar to above: https://stackoverflow.com/a/53582533/4924078

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



回答4:


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



回答5:


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


来源:https://stackoverflow.com/questions/32434516/vba-access-check-if-parent-form-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!