The first thing to understand about Sub-Forms 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 Sub-Forms, 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. In this article we refer to Subform as indicating the control, and Sub-Form as indicating the form on that control.
NB. A very common confusion when referring to items on a form is caused by the assumption that any Tab controls used are included in the reference. That is simply not the case. Whether an item (This includes, but is not limited to, Subforms.) is shown directly on the main form itself or on a Tab control doesn't effect the reference address at all.
The Internal syntax (code running within the form's module itself or one of its Sub-Forms) for this is :
Expand|Select|Wrap|Line Numbers
- Me.[SubformName].Form.[ControlName]
Expand|Select|Wrap|Line Numbers
- Forms![FormName].[SubformName].Form.[ControlName]
- Forms("FormName").[SubformName].Form.[ControlName]
- Form_FormName.[SubformName].Form.[ControlName]
Expand|Select|Wrap|Line Numbers
- Me.[SubformName]![ControlName]
- Forms![FormName].[SubformName]![ControlName]
- Forms("FormName").[SubformName]![ControlName]
- Form_FormName.[SubformName]![ControlName]
- FormName is the name of the main form.
- SubformName is the name of the Subform 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 the Exclamation Mark, or Bang, (!) used instead of the Dot (.) 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. It's important to understand though, that each form in a database is a specific class in its own right. EG. Form_frmA is a defined class and, although the Form class contains no controls as specific properties per se, Form_frmA probably will do. Hence, it is quite acceptable and correct to refer to controls on a form using the dot (.) syntax (EG, frmA.txtBlah).
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, Me.txtJobNumber 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 Sub-Form 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 Subform controls, [sForm B] & [sForm C], then referencing a control [ControlD] on [sForm C] from [sForm B] would be done as Me.Parent.[sForm C]!ControlD.
Examples to refer to the Sub-Form's txtJobNumber control where the Form is called frmFieldServiceDatabase and the Subform is called [qryReviewJobs Subform] (NB. Brackets [] are only necessary if, without them, the reference may have a different meaning. In these examples [txtJobNumber] uses them interchangeably, yet for [qryReviewJobs Subform] they are necessary, and without them the reference would fail.) :
Expand|Select|Wrap|Line Numbers
- Me.[qryReviewJobs Subform]![txtJobNumber]
- Me.[qryReviewJobs Subform].Form.txtJobNumber
- Forms![frmFieldServiceDatabase].[qryReviewJobs Subform]![txtJobNumber]
- Forms("frmFieldServiceDatabase").[qryReviewJobs Subform]![txtJobNumber]
- Form_frmFieldServiceDatabase.[qryReviewJobs Subform]![txtJobNumber]