Power Subform Techniques
Many new Access developers don't know the ins and outs of creating and modifying a subform and referring to subform controls, so let's first look at some important points you should know when working with subforms:
- The easiest way to add a subform to a main form is to open the main form, and then drag and drop the subform onto the main form.
- The subform control's LinkChildFields and LinkMasterFields properties determine which fields in the main form link to which fields in the subform. A single field name, or a list of fields separated by semicolons, can be entered into these properties. When they are properly set, these properties make sure all records in the child form relate to the currently displayed record in the parent form.
Referring to Subform Controls
Many developers don't know how to properly refer to subform controls. You must refer to any objects on the subform through the subform control on the main form, as shown in this example:Forms.frmCustomer.fsubOrders
This example refers to the fsubOrders control on the frmCustomer form. If you want to refer to a specific control on the fsubOrders subform, you can then point at its controls collection. Here's an example:Forms.frmCustomer.fsubOrders!txtOrderID
You can also refer to the control on the subform implicitly, as shown in this example:Forms!frmCustomer!subOrders!txtOrderID
Both of these methods refer to the txtOrderID control on the form in the fsubOrder control on the frmCustomer form. To change a property of this control, you would extend the syntax to look like this:Forms.frmCustomer.fsubOrders!txtOrderID.Enabled = False
This code sets the Enabled property of the txtOrderID control on the form in the fsubOrders control to False.