The Final Post and Continued from Last Week's Post.
MS-Access Application Objects (Table, Query, Forms, Text box, Label, etc.) need some meaningful names when we create them. If we don't, then MS-Access assigns default names, like Form1, Form2, Text1, Label1, and so on. These default names don't give any clue as to what those names represent. We are free to assign appropriate names in relation to what we are building in the Database. That way it is easy to remember those names (if not all of them), when we need them in calculations, in VBA, or in wherever they are referenced. We need these names to address them easily in VBA, like Forms!Employees!Salary rather than the usage Forms("Employees").Controls("Salary").Value.
Last week we started with a simple example, where we can use the symbol! , to shorten the length of the object addresses, when dot separators are used, giving you enough insight into what it is all about. When we have the Form’s name and Control name the expression can be written in a short form with the symbol! This is true when the Recordset fields are referenced, like rset!LastName to retrieve its default Property VALUE's contents, instead of rset.Fields("LastName").Value.
If you are a new visitor on 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: The frmSubForm when placed as SubForm on the frmMain becomes a control (with its own controls and properties) of the Main Form and is listed among the Controls list of the Main Form. If we make a list of controls of the Main Form, we can see that the frmSubForm is listed among them.
Open a Form with a SubForm then type the following command in one line, with changes in the Debug Window, and press Enter Key to get a listing of control names of the main form
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 contents, 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 it means? 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 re-write 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 is the .Form part in the above address means? It means that the Sub-Form Control created by Access is a control for loading a Form into it and it will be always 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.
On top of the list, all forms will appear, after that the list of Tables and then the Queries list. All the Tables are listed in Table.TableName format and queries with Query.QueryName format indicates the category of object you can insert into the Source Object Property of the Form 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 added 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
No comments:
Post a Comment
Comments subject to moderation before publishing.