Introduction
The first thing to understand about
SubForms is that, to add a form onto another form takes a special
SubForm control. This SubForm control acts as a container for the form that you want to act as a sub-form of the main one. That is to say, if you wanted
frmB to act as a sub-form of
frmA, then you would create a
SubForm control on
frmA (in this example we'll call it
sfmB).
SubForms have a .
Form property which contains a reference to the form required (
frmB in this case). It is very important when talking about
SubForms, to realise that the actual
SubForm is a control on the main form which contains a sub-form. It is not, in itself, the sub-form.
The
Internal syntax (code running within the form's module itself or one of its subforms) for this is :
- Me![SubFormName].Form![ControlName]
The
External syntax (code running from outside the form's module itself or one of its subforms) for this is :
- Forms![FormName]![SubFormName].Form![ControlName]
-
Forms("FormName")![SubFormName].Form![ControlName]
-
Form_FormName![SubFormName].Form![ControlName]
While theoretically the Form object of the subform should be included in the reference, there is a shorthand version available that leaves this off (it is assumed), so all the above versions can be shortened to :
- Me![SubFormName]![ControlName]
-
Forms![FormName]![SubFormName]![ControlName]
-
Forms("FormName")![SubFormName]![ControlName]
-
Form_FormName![SubFormName]![ControlName]
Where :
- FormName is the name of the main form.
- SubFormName is the name of the Sub-form control on the main form in which the actual sub-form is contained.
- ControlName is the name of the control that you want to refer to.
You will often see (!) used instead of (.) as well as vice versa. In most cases it's not a problem as Access will interpret it in the correct way. In general though, a dot (.) is used to denote a predefined property of the object whereas an exclamation mark (!) is used as shorthand to refer to a named item of a collection. Controls on a form, for instance, are members of the Controls collection of a Form. Fields in a RecordSet are actually members of the RecordSet's Fields collection.
The
Me object can be used within a Form or Report to refer to the associated Form or Report object itself.
EG. Within the module of a Form whose name is
frmTest, [b]Me!txtJobNumber[/M] is equivalent to
Form_frmTest!txtJobNumber.
The
Parent object is actually a reference to the object that this module is a child of.
Me.Parent from the module of a form used as a subform would therefore refer to the main form in the partnership. This can be used in conjunction with the subform references to access sibling forms. EG. If Form A has two subforms, Form B & Form C, then referencing a control (ControlD) on Form C from Form B would be done as
Me.Parent![Form C]!ControlD.
Examples to refer to the subform's
txtJobNumber control where the Form is called
frmFieldServiceDatabase and the SubForm is called
qryReviewJobs SubForm :
- Me![qryReviewJobs SubForm]![txtJobNumber]
-
Forms![frmFieldServiceDatabase]![qryReviewJobs SubForm]![txtJobNumber]
-
Forms("frmFieldServiceDatabase")![qryReviewJobs SubForm]![txtJobNumber]
-
Form_frmFieldServiceDatabase![qryReviewJobs SubForm]![txtJobNumber]