Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Access check if Parent form exists

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
like image 839
KameeCoding Avatar asked Mar 15 '23 20:03

KameeCoding


2 Answers

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.

like image 57
iDevlop Avatar answered Mar 31 '23 17:03

iDevlop


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
like image 27
Çağlar Durgun Avatar answered Mar 31 '23 15:03

Çağlar Durgun