KameeCoding
KameeCoding

Reputation: 723

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

Upvotes: 5

Views: 8288

Answers (7)

Tom Robinson
Tom Robinson

Reputation: 1930

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

Upvotes: 0

Moshe Klien
Moshe Klien

Reputation: 19

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

Upvotes: 2

Çağlar Durgun
Çağlar Durgun

Reputation: 49

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

Upvotes: 2

user4006507
user4006507

Reputation: 21

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

Upvotes: 1

Sergey
Sergey

Reputation: 243

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

Upvotes: 0

iDevlop
iDevlop

Reputation: 25272

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.

Upvotes: 5

Burkhard
Burkhard

Reputation: 51

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

Upvotes: 0

Related Questions