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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

difference between ! and . 3

Status
Not open for further replies.

scottian

Programmer
Jul 3, 2003
955
GB
can anyone tell me the difference between usin a '!' and a '.' in vba within access?
for example:-
Me![Field1] = 12
and
Me.[Field1] = 12
 
With "!" you reference objects in a collection. With "." you refer to properties and methods. Sometimes members of a collection are also properties (like form's controls). In this case both ways of referencing work. When the member is not a property of the object - dot fails.

combo
 
Hi scottian,

Very simply, dots (".") are used to denote Properties and Methods of an Object, and bangs ("!") are used to denote Members of Collections.

Controls on a Form are both members of the Controls Collection AND Properties of the Form. The two different syntaxes represent two different routes to the same object.
Me![Field1] is shorthand for Me.Controls("Field1")
Me.[Field1] refers to the Field1 Property.

If your control name contains a space, the Form's Property has the spaces replaced with underscores so Me![Field 1] and Me.[Field_1] both refer to the control called "Field 1".

It is marginally more efficient to use the Properties but also preferable because they appear in the Intellisense and are validated at compile time rather than run time.

Enjoy,
Tony
 
Explanations were perfect.

Now about using notations:

I would use
Me("Field1")

rather than
Me![Field1]
or
Me.[Field1]

Simply because the first way of referring to a control/field allows VB variables:

Function SetAValue(FormName, ControlName, SomeValueValue)
Forms(FormName)(ControlName) = SomeValue
End Function

Can't use the other notations to do that.

HTH



[pipe]
Daniel Vlas
Systems Consultant

 
What [ ] does is to EVALUATE whatever is inside it and that evaluation (for objects rather than variables) causes the DEFAULT property of the object to be used so for excel
[range] will return the VALUE of the range as that is the range default property
[Field] in access, would return the default property of a field (which I guess is the value again)

Rgds, Geoff
[blue]Si hoc signum legere potes, operis boni in rebus Latinus alacribus et fructuosis potiri potes![/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
Well, I believe that is the normal behaviour of any notation:

Me.Field1
Me!Field1
Me("Field1") equivalent to Me.Controls("Field1")
Me![Field1]

will all return the default property of the field, which is indeed the value.

The brackets (in Access) are only needed if you have spaces in the field name(which is anyway highly unrecommendable):

Me![Field 1]

I saw somewhere on Tek-Tips a post saying that what is in brackets is evaluated as a string and I agree with that.



[pipe]
Daniel Vlas
Systems Consultant

 
Not sure about Access but for excel - from the evaluate method help:

Note Using square brackets (for example, "[A1:C5]") is identical to calling the Evaluate method with a string argument. For example, the following expression pairs are equivalent.

[a1].Value = 25
Evaluate("A1").Value = 25


Rgds, Geoff
[blue]Si hoc signum legere potes, operis boni in rebus Latinus alacribus et fructuosis potiri potes![/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
Let's keep in mind the difference between object and its default property, in excel [A1] is still range:
x=[A1] returns value in "A1",
Set y=[A1].Offset(1,0) returns range "B1".
The idea with [ ] in excel and access is probably the same - it returns object with name or identifier in between (however, with formula, it can also return calculation result).

When returning an item from a collection, as Item is the default property of collection, we can use:
coll1.Item(n) or coll1(n) for short.
When the collection has named items:
coll1.Item("obj_name") or coll1("obj_name") for short
or with "!" operator:
coll1!obj_name or coll1![obj_name] if name consists spaces.
This is universal.

combo
 
Hi All,

I've never been entirely sure of the full effect of using brackets in Access, but here, briefly, is my best shot.

Dan is right to say they are needed when a field name contains spaces but, more generally, they force an interpretation process which may differ from the default in that Access always tries to interpret the string inside the brackets as a literal (usually a column name) first. For this reason, a column name the same as a reserved word (e.g. Date) usually needs putting in brackets; and an identifier which could, in context, be a variable name needs to be in brackets if it is to be treated as a Field Name.

Enjoy,
Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top