How To Refer to a Subform Control or Property From VBA
Almost every time I need to refer to a form's control or property, a subform's control or property, or a subform subform's control or property, I have to dig out the books. I posted this as a reminder so I can keep my books on the bookshelf.
Syntax
Refer to Subform Properties Forms![FormName]![SubformName].Form.SubformProperty Refer to Subform Controls Forms![FormName]![SubformName].Form![ControlName] Refer to Subform Control Properties Forms![FormName]![SubformName].Form![ControlName].ControlProperty Where SubformName refers to the name given to the subform control within the main form, and not necessarily the same name that appears in the navagation panel. Example 1:
Let's say we have a main form that needs to reference a textbox in a subform's footer.
In this case:
(1) The main form is frmMovementsHeader
(2) The subform is frmBeerMovementItems
(3) The total of the weight column in the subform (kept in the footer) is subTotalItemWgt. The formula in that textbox is =Sum([TotalItemWgt])
and that total is named subTotalItemWgt
To reference the value in the subform's subtotal from the main form, use this syntax in a textbox in the main form: =[Forms]![frmBeerMovementsHeader]![frmBeerMovementItems]![subTotalItemWgt] Example 2: Disable a subform so the user can't change any values in the subform: Me.frmBeerMovementItems.Enabled = True When using Subforms within Subforms, then use this syntax: Refer to Subform Subform Properties Forms![FormName]![SubformName].Form![SubSubformName].Form.SubSubFormProperty Refer to Subform Subform Controls Forms![FormName]![SubformName].Form![SubSubformName].Form![ControlName] Refer to Subform Subform Control Properties Forms![FormName]![SubformName].Form![SubSubformName].Form![ControlName].ControlProperty Where SubformName and SubSubformName refers to the name given to the subform controls within the main form, and not necessarily the same name that appears in the navagation panel. See Page 626 of Microsoft Office Access 2007 Bible