Detecting if a form is being used as a subform

  • Thread starter Thread starter David C. Holley
  • Start date Start date
D

David C. Holley

Is there a way, other than trapping for error 2452 - (The expression you
entered has an invalid reference to the Parent property) to determine
wether or not a form is being used as a subform.

On error resume next
t = me.parent.name
if err = 2452 then Debug.print 'Form is not being used as a subform'
 
I don't see what's wrong with what you wrote, other than the need to
reset the error handler after the test. You could use the generic
IsLoaded function to test the name of the form, but that doesn't
eliminate the possibility that the form is open both as Main and
Subform. How about:

air code:
----
Public Function bIsChildForm(f as Form)
On Error Resume Next

Dim strBuffer as String

strBuffer = f.Parent.Name

If Err <> 2452 Then 'invalid ref to Parent prop
bIsChildForm = True
Else
Err.Clear
bIsChildForm = False
End If

End Function
---

This way, you can call:

If bIsChildForm(Me) Then
. . .
Else
.. . .
End If

HTH,
Kevin
 
Access doesn't include Subforms as members of the "Forms that are open"
collection. Therefore, IsLoaded tells you whether something is or isn't open
as a Main form/report. That's all it does. IsLoaded results are meaningless
when dealing with subforms. (Not to mention that you can't establish a
reference to a subform by it's form name which is all IsLoaded has to work
with.)

Your point about making sure err.value gets reset is quite valid.
 
Back
Top