Reputation: 853
I have a form that in certain situations doesn't have a parent.
Then it fails to open.
So I wrap it in an 'if'
It still fails to open.
Private Sub Form_Open(Cancel As Integer)
If Not Parent Is Nothing Then << Error
Me!Button1.Enabled = Not (Nz(Parent!Lock, 0))
Me!Button2.Enabled = Not (Nz(Parent!Lock, 0))
End If
End Sub
gives "Run-time error '2452': The expression you entered has an invalid reference to the Parent property."
But if I declare the variable, then the original code won't work.
Upvotes: 1
Views: 63
Reputation: 32642
This is a tricky one, as if the form has no parent, any access to the property (even testing if it's empty) will trigger an error.
The easiest workaround is testing using error trapping:
Public Function HasParent(obj As Object) As Boolean
On Error GoTo ExitFunction
If Not obj.Parent Is Nothing Then
HasParent = True
End If
ExitFunction:
End Function
Private Sub Form_Open(Cancel As Integer)
If HasParent(Me) Then
Me!Button1.Enabled = Not (Nz(Parent!Lock, 0))
Me!Button2.Enabled = Not (Nz(Parent!Lock, 0))
End If
End Sub
Upvotes: 1