The thing is that a subform may contain another subform, to about 7 levels
deep now (from memory.) Therefore you need to use code that handles the
subforms recursively in order to know if it is loaded at any level.
The code below is untested, but see how you go. It loops through the Forms
collection, examining each open form. If it is not the form you are looking
for, it calls SubformSearch() which loops through its controls to test any
subforms. If the subform is the one you are looking for, it flags it as
loaded. If the subform has no form in it, it ignore it. Otherwise
SubformSearch() calls itself to examine the subform in that subform, and
continues to to so recursively so it handles all depths of nested subform.
Public Function IsLoadedAsFormOrSubform(strDoc As String) As Boolean
'Purpose: See if the form is loaded as a main form or subform.
'Return: True if open in any view, else False.
'Argument: Name of the form to search for.
'Dependency: Calls SubformSearch() recursively to handle subforms.
Dim frm As Form 'Each form
Dim bFound As Boolean 'Flag if found
For Each frm In Forms
If frm.Name = strDoc Then
bFound = True
Else
Call SubformSearch(strDoc, frm, bFound)
End If
If bFound Then
Exit For
End If
Next
IsLoadedAsFormOrSubform = bFound
End Function
Private Function SubformSearch(strDoc As String, frm As Form, bFound As
Boolean)
'Purpose: Searches each subform recursively for the named form.
'Return: None
'Arguments: strDoc = name of form to find
' frm = form to search
' bFound = Flag set to True if found.
Dim ctl As Control 'Each control on the form.
For Each ctl In frm.Controls
If ctl.ControlType = acSubform Then
Select Case ctl.SourceObject
Case strDoc
bFound = True
Case vbNullString
'do nothing
Case Else
Call SubformSearch(strDoc, ctl.Form, bFound)
End Select
End If
If bFound Then
Exit For
End If
Next
End Function