Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

basic concept yet confusing! Me.txtName vs. [forms]![thisform]![txtNam

Status
Not open for further replies.

tobymom

Programmer
Aug 18, 2006
36
US
Would someone please explain to me the differences between

Me.txtName

vs.

[forms]![thisfrm]![txtName]

also what's the difference between

Me.txtName vs. Me!txtName

I am really confused about using dot (.) and bang (!)

it seems like both works the same?
 
Search this forum for dot vs bang

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
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

Bang
objectName.collectionName!collectionindex

dot
objectName.collectionName.item("collectionIndex")

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.
 
Oh and to add just a little more confusion. All collections have a numbered index and some (not all) collections have a name index.

If the first item in the controls collection is "ctrlOne" it can also get referenced like:
Me.Controls(0).value
Me.Controls("ctrlOne")

For me this is another reason I use dot with collections because bang only works with collections with named indices.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top