Me" always refers to the form that the code is contained in.
[forms]![thisfrm]![txtName] refers to the open form named "thisfrm". It could be the same form the code is contained in, but programmers usually use this syntax to refer to another form (since the shorter "Me" would other wise be sufficient).
I have never seen this one answered correctly on this site for two reasons:
1)VB/VBA default properties and methods
2)Some unique properties in Access VBA not seen in other VB/VBA
VBA gives you two types of notation when working with collections: bang and dot as PHV said. You can mix them as well to add more confusion. Also vba has default properties and methods which you do not have to specify which gives more possible notations. Normally bang notation is only for collections (except in Access special cases). The basic structures are
So for example you have the controls collection which is a collection of all controls on a form or report. Both bang and dot allow you to reference the current instantiated class with Me (has nothing to do with differences of bang or dot). Usually we think of this as referring to a form or report from the form's or report's module (but this works with custom classes and other classes with modules such as worksheets in Excel)
To refer to the value of a control named "ctrlOne" on a form/report using bang the full notation is
Me.Controls.Item("ctrlOne").value
Just so happens that the value property is the default of a control and thus not required so you can drop it
Me.Controls.Item("ctrlOne")
It happens that "Item" is the default property of a collection and thus not required so you might see
Me.Controls("ctrlOne")
Just happens that the Controls collection is the default of a form or report so you might also see
Me("ctrlOne")
All of the above work as well as combinations.
And to add a little more confusion a control is an item of the Controls collection, but each control is also a direct property of the form so you can just reference the property directly with
Me.ctrlOne
Normally in VBA bang notation is for collections only (except in Access where there are some special cases).So using the bang and all of the defaults
Me.Controls!ctrlOne.value
value is a default
Me.Controls!ctrlOne
Controls is a default
Me!ctrlOne
Brackets are only needed if you use a bad naming conventions or referencing an object from a query.
i.e spaces:
ctrl One
reserved words:
Date
Now here is the only exception for bang that I know of. In all of vb/vba bang is for collections only, but in access you can refer to a field on a form or report (there is no such thing as a fields collection for a form or report, but there is for recordsets). So if you have "fldOne"
Me!fldOne.value
Me!fldOne
works to return the field value
Now the argument of which to use. Some people say bang is faster, but I think that is irrelevant. I also have seen it argued that it is faster to drop references to default properties, and again that seems irrelevant unless you are really concerned about a millisecond here and there.
I personally only use dot. For one reason, you can use variable names in dot and not in bang.
dim myControlName as access.control
myControlName = "ctrlOne"
msgbox Me.controls(myControlName).value
I also try not to use a lot of default properties because it makes it clear what I am doing. Example:
Me.Controls("ID").value
vs
Me.ID
The first clearly shows I want the value of a control named "ID". In the second it could be a field value, a control value, or the actual control object.
I will sacrfice clarity and flexibility for a few milliseconds.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.