The Final Post and Continued from Last Week's Post.
In Microsoft Access, application objects such as Tables, Queries, Forms, Text Boxes, Labels, and others should be given meaningful names when created. If we don’t, Access assigns default names like Form1, Form2, Text1, Label1, and so on. These defaults provide no real indication of what the object represents. Assigning descriptive names related to their purpose makes it much easier to recognize and remember them later—especially when they are used in calculations, VBA code, or other references.
For example, in VBA, we can directly reference a control like this:
Forms!Employees!Salary
instead of the longer form:
Forms("Employees").Controls("Salary").Value
Last week, we began with a simple example that showed how the exclamation mark (!) symbol can shorten object references compared to using multiple dot separators. Once the form name and control name are known, the expression becomes more concise with the '!' symbol.
This shorthand also applies to Recordset fields. For instance:
rset!LastName
is equivalent to:
rset.Fields("LastName").Value
and both return the contents of the field’s default Value property.
If you are a new visitor to this page and topic, then please visit the earlier two pages and continue from here. The links are given below:
- Dots and Exclamation Marks Usage with Objects in VBA
- Dots and Exclamation Marks Usage with Objects in VBA2
Referencing Form's Controls.
I will repeat the first example here, introduced on the first page of this three-page series, to go further in this discussion.
? Forms!frmMain!frmSubForm.Form!frmInsideSubForm.Form!Text7 ' 'The above command without the use of the ! Symbol, you must write it in the following manner to get the same result. ' ? Forms("frmMain").Controls("frmSubForm").Form.Controls("frmInsideSubForm").Form.Text7.value
Note: When it frmSubForm
is placed as a subform within frmMain
It becomes a control on the main form. Like any other control, it has its own set of properties and contains its own controls. If you generate a list of the controls on the main form, the subform will appear in that list just like a textbox, button, or label.
To see this in action:
-
Open a form that contains a subform.
-
In the Debug Window, type the following command on a single line (adjusting the form name as needed), then press Enter to display the list of control names of the main form:
This will print the names of all controls on the main form, including the subform control itself.
for j=0 to forms!frmMain.controls.Count-1:? j, forms!frmMain.controls(j).name:next
'Result of the above command, on my Form. ' 0 Label0 1 Text1 2 Label2 3 Text3 4 Label4 5 frmSubForm 6 Label5
The frmSubForm is listed as a Control of the frmMain with index number 5
Now, about the example given at the beginning, we have three open Forms: frmMain, frmSubForm & frmInsideSubForm, layered one inside the other. We are trying to print the Text7 Text Box value from the innermost form in the debug window. Look at the command given below:
? Forms!frmMain!frmSubForm.Form!frmInsideSubForm.Form!Text7The actual address of Text7 Textbox, all elements are joined with the symbol!, except the '.Form' after the name frmSubForm and frmInsideSubForm. This command will work without the '.Form' part. Try the command given below.
? Forms!frmMain!frmSubForm!frmInsideSubForm!Text7
If the address works without the .form part, why do we need it in the address, and what does it mean? It works without an explicit reference because the system knows that it is a Sub-Form control by default.
When you drag and drop a Sub-Form onto the Main Form, Microsoft Access creates a container control on the main form and inserts the Sub-Form into it. To be more specific, if you select the outer edge of the sub-form control, you can select this container control. Display its Property Sheet(F4) and check the Source Object Property setting. You can see that the subform's name is inserted there. This is the control where we set the Link Master Fields and Link Child Fields properties to set the relationship between data on the master form and sub-form.
You can rewrite this property value with any other form's name to load another form into it, in real-time. When you do that, consider the relationship change if the new form's source data is not related.
Coming back to the point, i.e., what does the '.Form' part in the above address mean? It means that the Sub-Form Control created by Access is a container control for loading a Form into it, and it will always be a form control, whether you explicitly add the '.Form' part in the address or not.
Loading Table or Query in a Sub-Form Control.
But the interesting part is that you can insert a Table or a Query (not an Action Query) into this control as well.
Try that, if you have a Form with a sub-form, open it in design view.
Click on the outer edge of the Sub-Form to select the Sub-Form control.
Display the Property Sheet (F4) and select the Source Object Property.
Click on the drop-down control to the right of the property to display the available forms, Tables, and Queries.
At the top of the list, you will see all Forms. These are followed by the list of Tables, and then the list of Queries. Tables are displayed in the format:
Table.TableName
and queries appear in the format:
Query.QueryName
This notation indicates the category of object that can be assigned to the Source Object property of the subform control.
Select a Table or Query to insert into the Source Object Property of the Sub-Form control.
Save the Form and open it in Form View.
Try to print the value of one of the fields displayed in the debug window.
Tip: It will print the value of the active record in the sub-form, if selected, otherwise the first record field value.
You will find the Table or Query result displayed in the Sub-Form control.
Is this the command you have typed in the Debug Window?
? Forms!frmMain!frmSubForm.Table!LastName
Then you are wrong, it is not a Table Control, still, it is a Form control only. When you set the Source Object Property Value with a Table's name, the system already adds the category name to the object's name (Table.TableName or Query.QueryName) to identify what type of object is loaded into the sub-form control.
So the correct command is:
? Forms!frmMain!frmSubForm.Form!LastName ' 'OR ' ? Forms!frmMain!frmSubForm!LastName