bang versus dot (1 Viewer)

Guus2005

AWF VIP
Local time
Today, 11:18
Joined
Jun 26, 2007
Messages
2,645
I always use a dot and never an exclamation mark.
Is there a difference? Is one faster?
And line 1000 compared to 2000?

Example:
Code:
1000        frmProgress!cboProcesstatusG = rs!StatusProces
1001        frmProgress!txtDatVooraanVerzondenG = Format(gsJAL_G!DatVooraanVerzonden, "dd-mm-yyyy")
versus
Code:
2000        frmProgress.cboProcesstatusG = rs.Fields("StatusProces")
2001        frmProgress.txtDatVooraanVerzondenG = Format(gsJAL_G!DatVooraanVerzonden, "dd-mm-yyyy")

Thanks!
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 06:18
Joined
May 21, 2018
Messages
8,463
This has been answered many times but most of the explanations are incorrect, incomplete, or simply a consequence of the below. Bang does one thing only

It causes the runtime to invoke the default member of an object and pass the name following the bang as a string argument.

I have never seen anything that would suggest any difference in any real terms. Obviously Dot has more advantages with ability to use variable names and intellisense. Bang has only one advantage in that it is nice short hand. And in certain "expressions" such as queries, calculated controls, and conditional format you have to use this form. Bang does one thing. It causes the runtime to invoke the default member of an object and pass the name following the bang as a string argument. Thus
RS!FieldName is the same as RS.fields("FieldName") since fields is the default member of the recordset collection.

The dot is early-bound and the bang is late-bound, not sure what the impact is in practical terms but that can cause problems with debugging.
me!name will compile and then error at runtime 'Not Desired
me.SomeMispelledControlname will not compile 'Desired
me!someMispelledControlName will compile 'Not Desired

They are not equivalent beyond that. The dot is the one and only way to access a named public member (property, method) of an object not through a collection.

The one seemingly apparent exception is the fields on a form. The default member of a form is the controls collection and there is not even a fields collection. This was explained by Sonic8 in that the fields of the recordsource are added to the default controls collection as a pseudo control in a special way. They do not appear in the control count.
 

vba_php

Forum Troll
Local time
Today, 05:18
Joined
Oct 6, 2019
Messages
2,884
I always use a dot and never an exclamation mark.
Is there a difference? Is one faster?
And line 1000 compared to 2000?
Guus,
I think MS has seriously screwed this syntax up, because I've run into countless errors from version to version and object to object with those different version where sometimes bangs work and sometimes periods work. I believe i wrote a FAQ on this too, like years ago, but it addresses referencing subforms controls and the syntax that works and syntax that doesn't work. and even in there with all my testing, some of my examples use "." and some use "!".

I believe it's different when using them in the GUI properties vs. using them in code. I don't really believe there is any consistency to any of this, given the fact that, again, countless people have posted questions on this forum addressing this same issue. and their question usually revolves around the fact that their syntax errors out. I would doubt that MS's websites could clarify this to any degree. After all, Access is not profitable for them so they probably pass the development off to low-level programmers that don't pay much attention to detail.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:18
Joined
Feb 19, 2002
Messages
42,981
You are correct, not all parts of Access are on the same page regarding the use of bang vs dot. In the early days, there was a much more rigid separation. That has blurred over the years but not all the programmers on the team got the memo so you will see differences within Access. Especially in SQL vs VBA. SQL is more likely to adhere to the original rules and VBA is more likely to be sloppy and allow variations some places but not others.

My understanding is that dot is used when referencing "Access" methods and properties and bang is used when referencing user defined methods and properties. This may be just a different way of saying what Maj quoted. One thing that has changed over the years is that fields from a bound recordsource are now considered part of the form/report's object model and are referenced using dot. In earlier versions of Access, if you had a field that was not bound to a control, you had to refer to it as me!someunboundfield, you could NOT refer to it as Me.someunboundfield. This leads us to why many developers use different names for controls than the fields they are bound to. If you have fieldA bound to a control named txtFieldA, you will see the difference in intellisense. Me.fieldA will show only the methods and properties of a field in the bound RecordSource whereas Me.txtFieldA will show the methods and properties of a control. This distinction allows you more precision when coding but it can lead to strangeness. For example if you code
Me.FieldA = "x"
the value will NOT show up in the control but it will modify the RecordSource
However:
Me.txtFieldA = "x"
will show up on the form, and when the record is saved, will be applied to the RecordSource.

In the early days:
Me!somefield. would NOT pop up intellisense
But
Me.somefield. would pop up intellisense

Today, both give you intellisense.

However, I believe the difference remains that dot is early binding and bang is late binding. The result of that is you might not see certain types of errors as you are coding. They will only show up at runtime. This is dangerous in my opinion so I always use dot as long as Access allows it. Unless your testing is sufficiently detailed so that you are certain that every path of every if is actually tested, using bangs may let errors slip through that are not discovered until some user runs into the other path.

The biggest difference in the two pieces of code in the initial question is the lack of using "Me." Using Me. when referencing form/control objects clarifies for the interpreter exactly which library holds the definition for that object. Without the "Me.", Access has to search multiple libraries to find where something is defined. it starts with the current procedure, works out to the current module, and then to other modules in the project. Then it goes to the Access library and if necessary to any other loaded modules. "Me." localizes this search to the form/report object. The time saving is minuscule but it all adds up.

Also, without the "Me.", you may not see intellisense at all.
 
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Today, 06:18
Joined
May 21, 2018
Messages
8,463
My understanding is that dot is used when referencing "Access" methods and properties and bang is used when referencing user defined methods and properties
That is in no way correct and not another way to say what I said. I think you may mean this in reverse, but still that is would not be correct. The definition I provided is 100% correct. There is no way ! works on a method of either an access or user defined object. There is no property that this works on that is not the default property. There is no property that this works on that is also not a collection.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 21:18
Joined
Jan 20, 2009
Messages
12,849
Have a look through this thread.

https://www.access-programmers.co.uk/forums/showthread.php?t=202806&highlight=bang+vs+dot

It addresses it better than I could because I'm sure I don't remember all the reasons why you choose one over the other at a given moment. In particular, Galaxiom has a post fairly early in that thread that goes over all the choices and meanings.

Unfortunately it was still in early days of my experience and although largely functionally correct it is not technically correct.

Have a look at what I posted in this thread.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 05:18
Joined
Feb 28, 2001
Messages
27,001
Thanks, G. I missed the later thread in my search for Guus, but what the heck, he's getting a pretty good answer now! (or is that now.) ?
 

Guus2005

AWF VIP
Local time
Today, 11:18
Joined
Jun 26, 2007
Messages
2,645
Thanks everyone, especially Pat!

I have always preferred the dot above the bang because a dot always showed intellisense. I wasn't aware that nowadays the bang also shows intellisense.

Secondly i was searching for a reason to always use Me in front of controlnames. Your explanation is very clear. I know that my code runs a tiny bit faster but every millisecond adds up.
It is just good practise to do it this way.

Code:
with me
    .cboSomething.enabled = True
    .chkActive.visible = True
end with

I am creating a document about good practises in Access programming to normalise the coding and to always use fast code. e.g. To (almost) always use the faster domain function alternatives (Tlookup) and use the string function like Trim$, Left$ and such. There was a microsoft document once but i can't find it anymore. (this is not a new question)

These remarks will definitely find a place there.

Thanks again!
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:18
Joined
Feb 19, 2002
Messages
42,981
You're welcome.
I think the "with" construct is over used. Some programmers always use it. Others, like me, use it only when I actually have a list of references to the same parent object so i would probably draw the line at 3+ items and I NEVER use the construct for "Me." so mostly I use it for referencing recordsets and doing OLE automation.

I think the aversion revolves around how the construct affects code nesting and also the confusion of nestsing with's.
 

Users who are viewing this thread

Top Bottom