how to tell if a subform is open?

G

Guest

When I place the name of a root form in the following code, i get a "loaded"
however if I place the name of a subform in it i get "not loaded" (the
subform is open, etc). How can I tell if a subform is open?

Private Sub test_DblClick(Cancel As Integer)
If (CurrentProject.AllForms("sample_collection").IsLoaded = True) Then

MsgBox "loaded"
Else
MsgBox "not loaded"
End If
End Sub


thanks!
 
A

Arvin Meyer [MVP]

A subform is a control on a form, like a textbox. It doesn't become a form
(or a member of the forms collection) until it gets focus. The control,
however has a form property which you can refer to in code. I don't know if
your code will work with it because your code's argument requires a string.
The form property is referred to like:

Me.NameOfSubformControl.Form.Requery
 
G

Guest

nice explaination.

Well maybe you can just help me with the problem.
I have code on a form (A) that I want to trigger only if another form ( a
subform to a different form) is loaded. Do you have the syntax for code that
I can I put in the Current event of form (A) so that the code will run only
if this other subform is loaded, e.g. an if/then statment? something like if
the subform is open, loaded or something, then run the code

thanks
 
D

Douglas J. Steele

Can the form that contains the subform in question be open without the
specific subform on it? In other words, do you have code that changes the
SourceObject property of the subform control? If not, then why not simply
check if the parent form is open?

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)
 
D

Douglas J. Steele

For that matter, if you are changing which form is being displayed in a
particular subform control, you could always check the SourceObject property
of the subform control on the parent form (once you've checked that the
parent form is open)

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)
 
A

Allen Browne

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top