Determining if subform is opened as mainform

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there some type of flag I can conditionally execute code if a form is
actually a subform?

Situation is:
Mainform
Subform1
Subform2

I requery Subform2 from the on current event of Subform1. This works fine.
However, when I open Subform1, I get an obvious error. Would like to
conditinally bypass code on Subform1 if it is opened as a mainform.

Thanks...
 
I don't know if there's something more elegant, but you could certainly
include a reference to parent.name and catch the error generated (i.e. if
there's no parent form).
 
The following code in the form you want to conditionally execute code
depending on whether the form is a subform or not should help. When a
form is opened as a subform it has a parent otherwise it doesn't. By
testing for the parent you are seeing if the form is a subform. You
need to use on error resume next around it because if the form is
opened as a main form it will not have a parent - and will cause an
error when you try to refer to the non-existant parent.

On Error Resume Next

Dim strParentName As String

strParentName = Me.Parent.Name

If Err.Number <> 0 Then
'Form is not a subform
MsgBox "Form is not a subform"
Else
'Form is a subform
MsgBox "Form is opened as a subform of " & strParentName
End If
 
Back
Top