In a previous post (Bang Vs. Dot In DAO), I wrote about the difference between the Dot (.) and the Bang (!) in DAO. It's pretty straight forward. Dot is used to separate one level of the DAO hierarchy, separating an object from its methods and properties. Bang is used to separate an object from the collection in which it is contained.
This is true as far as it goes, but two types of objects in Access, Forms and Reports, muddy the waters considerably. Because form and reports are classes, controls on them are members of both the Objects Collection and a property of the form or report itself.
You can verify this by creating a new form or report object and looking at the Object Browser in the Visual Basic Editor.
(While it works the same on reports, I'm going to concentrate on forms for the moment.)
Create a new form: Form2 with no controls or Record Source. Open the Visual Basic editor and push <F2> to open the Object Browser . To the right, you'll see a list of Classes and Members. These members represent the properties and methods and properties of the selected Class. Access creates a number of default methods and properties which I'll ignore for now.
Next, create a new table: Table1(Table1ID, Field1, Field2). (See Figure 1)
Figure 1: Table1
(Note: throughout this post, my form's name will be MyForm and the control is called ControlName -- it could be any control, a textbox, combobox, label, or whatever)
Make this table the RecordSource for Form2. Table1ID, Field1, and Field2 appear in the member list. This demonstrates that the fields in the record source behind the form are properties of the form. See Figure 2.
Figure 2: Table1ID in the members list of the Form2 class
Next I'll reference the fields behind the form. To reference an object on a form, you start with the Forms collection, followed by a Bang (!), followed by the form name. Like this Forms!Form2. This gives me a reference to the form itself.
Now, according to my definition above, following the form reference with a dot and the field name should work (because the fields are properties of the form) but the bang should not because I haven't created any controls yet. However, on testing, I find that both:
Forms!Form2.Table1ID
and
Forms!Form2!Table1ID
Return the value of Table1ID.
But even though they produce the same result, they aren't the same. It's really a case of two objects that mean different things but nevertheless almost always give the same result. The bang (!) notation specifically denotes that what follows is a member of a collection; in this case, a member of the form object's default collection, the Controls collection. The dot (.) notation denotes that what follows is a property or method of the preceding object.
ME Object
And then, just to muddy the waters even further, there's the "Me" object. The Me object is used in Visual Basic for Applications (VBA) to reference an instance of a class module. It is an implicitly declared variable and is available to every procedure within the class module and only within the class module.
Since Access Form and Reports Modules are classes, you can also use the Me object to refer to the Form or Report itself. This allows us to take a shorthand reference to object on a form. I'll address form referencing in a later post, but for now, I can reference a control on a form explicitly:
Forms!MyForm!ControlName
Or
Me!ControlName
But as I said, the Me object muddies the water because Me.ControlName also works.
I know why Me!ControlName works. It is really just a short-hand way of referring to the default collection and property of the Form object.
The Controls collection is the default collection of the Form object, and Items is the default property of the Controls collection. An explicit reference to a control looks like either of these:
Me.Controls.Item(0) (assuming 0 is the correct index)
Me.Controls.Item("ControlName")
Since Item is the default property, you can also do these:
Me.Controls("ControlName")
Me.Controls!ControlName
and since Controls is the default collection, you can do these:
Me("ControlName")
Me!ControlName
So what about Me.ControlName?
This is the really cool part about forms -- when a form loads, it helps you out by adding every control on the form as a property of the form. This is why
Me.txtTextBox
.. works. You're asking for the "txtTextBox" property of Forms!MyForm -- which is a pointer to the control, in this case, the text box object.
Which should you use?
So, which is actually preferred? The answer is ... it depends.
Reasons to use Me Dot (Me.ControlName)
- Automatic Intellisense support.
- Runtime error if control is missing or mis-spelled.
- Slightly faster than Me Bang.
Reasons to use Me Bang (Me!ControlName)
- Me Bang ALWAYS works to reference the value of a control.
- If a control is named the same as a reserved word (i.e. "Name"), Me Bang will correctly reference the control.
- If the Record Source of a form is modified at run-time, Me Bang will continue to work.
- Intellisense can be initiated with <ctrl>+<space>.
No comments:
Post a Comment