Me! vs Me.

  • Thread starter Thread starter Craig
  • Start date Start date
C

Craig

I am new to Access but not development, so I have an Access specific
question.

I have a question about using Me!

If I have a form called MyForm, create an event and start coding and
referring to controls on the form, does the Me! qualifier really make a
difference? Like in Me!ProfileID.Text vs ProfileID.Text.

Should I use Me! at all?

Also what's the difference between Me!ProfileID and Me.ProfileID without the
! and which should you use?
I notice that when use Me. instead of Me! I get the popup menus with all the
possibilities which is nice.


Thanks
 
The best explanation of the Dot and Bang notation that I've seen was a
recent one provided by Access MVP Dirk Goldgar. Here it is:

"Bang" vs. "Dot" Notation
--------------------------

It's not so much a question of one or the other being "proper syntax",
but that they mean different things that nevertheless almost always give
the same result. As I understand it, 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. That would logically make the bang notation
"proper" and the dot notation improper.

But wait. Wherever possible, Access makes the controls on a form and
the fields in its recordsource all available as properties of the form.
It also makes the fields of the recordsource available via the bang
notation. I'm not sure exactly how it does this; maybe if a name is
not found in the Controls collection it checks the Fields collection of
the form's recordset as a fallback position. So for most practical
purposes Me!ControlName and Me.ControlName evaluate to the same thing,
and the timing tests I've seen suggest that there is little to choose
between them as far as execution efficiency is concerned. I seem to
recall that there is a very slight difference, but I can't remember
which way the advantage lies, and it's not much. There's a coding-time
advantage, however, to using the dot notation, as it makes the
"intellisense" dropdown lists available. That's a strong argument for
using the dot notation, in my book.

But wait again! I said above that Access makes the controls available
as properties "wherever possible". There are cases where it can't do
that. Specifically, it can't do it when there is already a property of
the same name as the control in question. For example, if your form
"Form1" has a control or a field foolishly named "Name", currently
displaying the value "John Doe", then executing this statement in the
form's code module:

Debug.Print Me!Name, Me.Name

will print

John Doe Form1

in the Immediate Window. So you must be careful not to use any reserved
words or built-in properties as names for your controls, if you want to
use the dot notation to refer to them. But then, you should avoid doing
that anyway, as it tends in general to confuse poor Access."
 
Bang (!) vs Dot ( . )

The Bang refers to collections, the Dot to properties. That said, many
objects can be both Collections and Properties. For example,
Me.Controls!ctlName is the same thing as Me!Controls!ctlName which is the
same thing as Me.Controls.ctlName ... confusing, but that's Access for ya.
The reasoning is this: Since a Control is an Object, but it is also a
Collection of the control objects on a form, you can refer to it by either
method.

And, as you have found, using the Dot syntax where possible does provide
you with Intellisense (the popup menus).
 
Do you even have to use Me. or Me! if referencing controls on current form?
Is there any advantage or speed issues favoring Me syntax?
 
Do you even have to use Me. or Me! if referencing controls on current
form?

It's possible but why would you want to do that?
Is there any advantage or speed issues favoring Me syntax?

From Access 2002 VBA Help: "The Me property contains an object reference to
the current form or report and is faster than a fully qualified object
reference."
 
I am confused. I don't want to use Me! syntax unless I have to.
I am not using it at the moment and all seems to work fine in the VB code
behind pages.

I can understand that it is faster than a fully qualified object reference
but if you are coding the current form you wouldn't use the fully qualified
object reference anyway. And if you are coding the current form why use Me
syntax? Isn't it understood when you code txtName.text = "Craig" that we
mean the current form?

So where do you have to use it?

Please excuse my newbie questions but something doesn't sound right.
 
We qualify controls with their form name or Me! (where applicable), declare
variables with notations such as "strMyText", "intMyNum", etc., because that
makes code "unambiguous".

Ambiguous code is confusing to those who may have to follow you to maintain
it (also will be confusing to you a year or more later!). Ambiguous code
is also expensive to employers/clients because it takes more hours to figure
out what the #@&(#@ programmer who wrote it was thinking. At some point
Access will fall over when code is ambiguous enough.

If you develop and use proper coding techniques even when you do not have to
use them, they will be second-nature to you when you *do* need them.
 
Another important issue that Dirk didn't mention is compilation.
If I have a form *without* a textbox name txtFoo and I code this ...
Me!txtFoo = "something"
It will compile without complaint. Of course it will break during
runtime.

If I do this...
Me.txtFoo = "something"
then it will not compile. I catch it before it gets to runtime.

On Intellisense:
Some will say that you can get Intellisense dropdown by using
{Ctrl}{Space}. But, what that yields is simply a listing of *all* the
items in the Object Browser. IMO, that removes the "intelligence" from
Intellisense.

- Jim
 
You are correct that in most cases, your form code does NOT need the me.

So, you can use

msgbox "last name = " & lastName

Or

msgbox "last name = " & lastName.value

Note that in VBA/ms-access we do not USE THE .TEXT property of a control
UNLESS THE CONTROL HAS THE FOCUS. So, use .value in place of .text when
moving from VB to VBA/ms-access.

The .text property of a control is certainly most useful in those events
that fire and the data has NOT been updated. For example, if you use the
keydown event, the control is not yet updated, and to get use/see the
current value of the control, you MUST use the .text, as the .value is NOT
YET updated. Thus, for general controls, and referencing their values, you
will as a habit use .value. However, for events and when the control has the
focus, you will use .text. However, for even before update, and after
update, you can use the value. So, .text use is quite rare, and generally
only for keypress type stuff.


Now, lets get back to our example:

msgbox "last name = " & me.LastName

The only reason why the above is more useful is that intel-sense popped up
the lastname field for you, and thus you will have much less of a chance
miss-spelling the field name.

Note that "me." simply refers to the current form, and thus if our form was
frmCustomer, then we could use:

msgbox "last name = " Forms!FrmCustomer.LastName

Now, when does use the me. come in handy? Well, lets assume we have some
button code where you press a button, but CALL SOME CODE in a library. (ie:
we want to re-use the code in a module for each time we use the button). You
thus can go:

Call MyCode(me)

At run time, "me" will be replaced with forms!frmCustomer.

Further, if you cut and paste the code into a DIFFERENT form, or even copy
the whole form, and continue to use the above code, then you DO NOT have to
re-write or change any code. So, the me. does refer to the current form code
that you are running. For stuff like field names etc you most certainly CAN
leave out the me. (but, it is quite handy as mentioned). And, if you need to
pass the current form name to another routine as in my above example, then
the form reference is NOT hard coded.

Having said all of the above, the real question here should be:

What kind of problems and differences will I experience as a developer when
using me. vs me!

Well, as others have mentioned, the me. syntax denotes properties and
methods of the form. These properties and methods of the form MUST BE
RESOLVED AT COMPILE TIME!

Thus, if you have a un-bound form (or a form where you set the record source
at run time), then the following will generate a compile time error

msgbox "last name = " & me.LastName

Why? Because we have NOT YET set the record source of the form, and thus
me.LastName will generate a compile time error if NO recordsource is set
(and we don't have a text box on the screen called LastName). Your solution
to this problem is either:

a) place a control on the form with the SAME NAME as the field(note that you
in general DO NOT need to place a control/text box on a form to
get/grab/use/update/reference a field name from the form underlying
recordset. However, if you SET THE FORMS REOCRDSOUCE AT RUNTIME, then you
MUST use me!LastName, as me.LastName does NOT yet exist!).

You can't use me.lastName, since it will NOT exist as a member of the form.
You cannot even use msgbox "last name = " & LastName. Ms-access will not
resolve the lastname field if the record source of the form is NOT set, and
a textbox called lastname does not exist on the form. If you try and compile
your code, you will see a compile error (again, this assume the form does
NOT have a datasouce yet set).

I have a considerable amount of code that actually sets the sql source of a
form at runtime. You can do this VERY EASY in code, and so I often do the
following:

me.RecordSouce = "select * from tblCustomers where city = 'Edmonton'"

Also, note how I use "me." RecordSouce, and thus you can instantly
assume/understand that the above code is running in a form! (you can't use
me outside of forms module). So, often, use of "me" does give you a hint as
to where the code is being run. So, this again supports that using me. often
conveys to you the reader "where" and in what context the code is being run.

Anyway, the above simply allows you to set/stuff sql into a form at runtime.
This means the forms datasouce is blank, and thus you CAN NOT use all the
fields of the reocrdsouce via the me.SomeField notation. It will NOT work!
You can only use the dot notation for fields that have the same name of a
text box on the form. Often, if not most of the time I DO NOT want to user
to see fields like the key id (autonumber). So, I do NOT place the
keyid(autonumber) field on the screen. With a normal bound form, you are
free to use me.ID, or me!id (both work despite the id NOT being placed on
the form). However, for un-bound forms (or forms with reocrdsouce set at
runtime) to use/grab the key id YOU CAN NOT USE me.ID. You MUST use me!ID

So, me!FieldName will NOT break in code, but it will ALWAYS compile. It is
certainly now a very good debate, since using me. will be ALWAYS be caught
at compile time, where as me! errors are ONLY going to be realized at
runtime. You can put in code me!SomeField even in the case where the
fieldname does not exist! Thus, me. represents built in and *existing*
properties of the form, where as me! is for user defined stuff.

The fact that we get all the fields as a property of a form via me. when it
is bound is a bonus.

So, in fact, me!Fieldname is more correct, as it means the value we are
using is NOT a ms-access defined property of the form, but is in fact a
programmer/developer defined property. So, you might consider coding to this
standard, as then you will instantly see when you are using a ms-access
defined property (.) or a user defined value(!).

So, really, one probably should use me! for your field names, and stick to
only using me. for ms-access defined stuff. However, since me. gives compile
time checking for your field names, and you also get inteli-sense, then a
good deal of us continue to use me., but in fact we should be using the more
correct me! for field names.

So, here is my rule of late:

For forms that set the reocrdsouce at runtime, then I use me!

For the rest, I continue to use me.

However, over time, I am now starting to learn towards the me!, as then if I
take a existing form, and start changing the record source, I will NOT get
errors if I decide to remove a text box from the form, but still need to use
the field in the underlying recordsource. And, also, if I change the sql
then I can safely delete textboxes on the form, and not have code break when
changing the recordsouce, or trying to reference fields that don't necessary
have a corresponding text box control. I stress again, this problem only
applies to forms where you set the datasouce at runtime, and they are blank
at design time). So, with more skill, the fact of catching at compile time a
bad field references like me.WrongFieldName is less of a bonus then having
code break, or being forced to place text controls on a screen to grab
underlying field values.

This whole issue is going to come down to how you code and design, and what
you find breaks, or does not break in code. So, your choice of me. really
comes down to your habits as a developer, but I am learning towards the me!,
as it is technically more correct.
 
Jim Allensworth said:
Another important issue that Dirk didn't mention is compilation.
If I have a form *without* a textbox name txtFoo and I code this ...
Me!txtFoo = "something"
It will compile without complaint. Of course it will break during
runtime.

If I do this...
Me.txtFoo = "something"
then it will not compile. I catch it before it gets to runtime.

Good point (and Albert's discussion also brings out the flip side of
it). Since I post that standard reply of mine from time to time, I'll
add these conmments to it, by your leave.
 
Good point (and Albert's discussion also brings out the flip side of
it). Since I post that standard reply of mine from time to time, I'll
add these conmments to it, by your leave.
Oh, by all means. This is a topic that comes up with some reqularity.
There is an Access Advisor article by Andy Baron about this that I
point people to sometimes. It's at ...

http://doc.advisor.com/doc/05352

Personally I tend to use the dot. Along with an object naming
convention I find it works well.

- Jim
 
Now that my colleagues have given you the factual, actual, useful answers,
I'll add a "silly" one:

"Me!" is more emphatic than "Me."

I remember that from elementary school (and, for me, that was a long, long
time ago).
 
Good Article!!

It also states....
"If your code is in the form's module, you can even use ControlName without
any prefixed reference to the form. As with any property of a class that
code runs in, VBA correctly recognizes the name"

To me this means that......
Me!ControlName.Value
Me.ControlName.Value
ControlName.Value

are all the same and none has any advantage over the other WHEN your code is
in the forms module.
 
Good Article!!

It also states....
"If your code is in the form's module, you can even use ControlName without
any prefixed reference to the form. As with any property of a class that
code runs in, VBA correctly recognizes the name"

To me this means that......
Me!ControlName.Value
Me.ControlName.Value
ControlName.Value

are all the same and none has any advantage over the other WHEN your code is
in the forms module.
You can add to the list...
ControlName

Since Value is the default property.

- Jim
 
Back
Top