Microsoft Access: Check if Form Object is a Subform
How do you check if the form object is a subform in Access without triggering a runtime error? Hint: this is a trick question.
As far as I am aware, there is no property on a form object that you can check to see whether it is either the topmost form or a subform.
The form object does have a Parent property. If the form is a subform, then it will have a parent object defined in that property. If it is the topmost form, though, any reference to the property will raise error 2452:
The expression you entered has an invalid reference to the Parent property.
In fact, we can't even check to see if the property is nothing without raising the above error:
The simplest option, then, is to isolate the IsSubform check in a standalone function and ignore any errors the function may generate.
The Code: IsSubform()
'Returns True if the passed form object is a subform of a parent form
Function IsSubform(Frm As Form) As Boolean
On Error Resume Next
IsSubform = (Not Frm.Parent Is Nothing)
End Function
Image by Andreas Wohlfahrt from Pixabay