Microsoft Access referring to controls on subforms

I recently replied to a question on a newsgroup and thought it was worth reproducing here.  The question was how do you refer to a text box on a form within a form within a form.  In other words, the main form has a subform and the subform has a subform.  To refer to a control that is on a sub sub form, you need to understand the following.

In the example there are three forms. frmMainForm, frmSubForm, frmSubSubForm

On frmMainForm is a subform control called subSubform Within that control the source object is frmSubForm. The thing to remember is that you refer to the control not the subForm within the control. You refer to Forms![frmMainForm]!subSubForm] not to Forms![frmMainForm]![frmSubform].

Think of it this way. If you only had one form, and you wanted to refer to the value in a textbox, you would refer to the textbox name, not the textbox data. If you had a textbox control called txtCustomer containing a table field name which might be tblCustomers.CustomerName, you would refer to the value as Me!txtCustomer rather than Me!CustomerName. Same with subForms. Refer to the control name, not the value (form name) within the control.

You have now referred to the control on the main form. The next step is to say what part of that control do you want to refer to? You want to refer to the Form part (Form object). You now add .Form to the path. Notice it is a dot rather than exclamation mark because it is not a field but a property. It could have been a property such as .Height or .Visible if you were referring to properties. If it was a control on the subform,you would have used “!” instead of “.”

Now you have made it to the subform, you want to get to the subsubform. Once again refer to the control on the subform which we will call subSubSubForm. We want to look at a control on the subsubform so we use .Form again and then the name of the control.

The full path is below.

Forms![frmMainForm]![subSubform].Form![subSubSubForm].Form![txtOnSubSubForm]

Forms – Collection of forms in the database
![frmMainForm] – We are referring to a user created object so prefix with an exclamation mark. The form we are interested in is frmMainForm
![subSubform] – Refer to a control on frmMainForm which holds a subform called frmSubForm. We don’t need to mention the subForm name, only the control name
.Form – The form that sits within the control is now the focus
![subSubSubForm] – Find a control on the subform that contains the second subform
.Form – The value we are interested in the subsubform is the form object itself
![txtOnSubSubForm] – Finally we reach the control on the subsubform that we want to use.

Hope all this makes some sense. Just remember you only ever refer to the main form by name. The rest of the form names are irrelevant. It is the control names that matter.

Tags: , ,

Leave a Reply