What does Me refer to in Access?

  • Thread starter Thread starter Nocturnal
  • Start date Start date
N

Nocturnal

I've noticed more and more code or various snippets of code which has Me in
it. What does that refer to and how does one use it?
 
Good question.

To reference a form, or a "text box" on a form, the normal syntax is:


forms!NameOfYourForm!NameOfField

If we are going to place a button on a form, then have it display last name,
you could write the following code:

forms!NameOfYourForm!LastNameTextBox

since a form might have a lot of code, then "thankfully" ms-access gives you
a "ready made" forms reference that you can use in a form. These "ready" to
use reference ONLY works in forms code modules, and does not work in a
stranded module. That reference is "me"

Hence, in he above

me = forms!NameOfYourform

So, anytime you need to reference a control on your form, you don't have to
type forms!NameOfYourForm over and over again like a broken record.

Hence, to display the last name as above, you can use:


msgbox forms!NameOfYourForm!LastNameTextbox

or

Msgbox me!LastNameTextBox

So, "me" is simply a shortcut way to reference the current form that the
code is running under. Note that "me" can ONLY be used in a forms module
code, and thus it simply represents the CURRENT form your code is running
under.

It saves you a ton of typing...help keep you sane....
 
Me is a reference to the form or report containing the code.

For example, in the code of Form1, you can use Me in place of:
Forms!Form1
but Me is better because:
a) It still works even if you rename the form.

b) It still works if the form is a subform.

c) It's more efficient (faster to type and execute, and better memory
usage.)

In most cases, the Me is optional. For example, this code:
Me.Text0 = 99
could be written as just:
Text0 = 99

But using Me makes it considerably easier to write and debug your code:
a) As soon as you type the dot after Me, the Intellisense offers the
possible values. This means less typing, and less chance of an inaccurate
spelling.

b) If you do mistype the word, or remove the control, VBA warns you when you
compile the code (Compile on Debug menu.) Otherwise you may not know
anything is wrong until the code runs, and perhaps not even then (if you did
not use Option Explicit.)

c) Anything that makes the code more specific and clearer is worthwhile. If
you just type:
Text0
Text0 could refer to:
- a control on the form,
- the Value (default property) of a control,
- a field in the form's RecordSource (even if there is no control),
- the value (default property) of a field,
- a variable (declared or undeclared) in the form's module,
- a public variable in another module,
- a keyword, constant, procedure, enumeration, type, collection, project,
etc, etc.

So, to write unambiguous code, and reduce the chance of misinterpretation or
flaws that won't show up at design time, you want to be as specific as
possible. Me gives you one way to achieve that. Option Explicit is another.
 
Being picky, its strictly speaking a reference to the current instance of the
*class*. You might have several instances of a form open for instance (no
pun intended!). Most of the time, however, you can think of it as a
reference to the form in whose module the code is running.

Ken Sheridan
Stafford, England
 
So, "me" is simply a shortcut way to reference the current form
that the code is running under. Note that "me" can ONLY be used in
a forms module code, and thus it simply represents the CURRENT
form your code is running under.

That's not technically correct. "Me" refers to the *class module* in
which the code is stored. That can be attached to a form or report,
or it can be a standalone class module.
 
David, I understand what you mean, but I'm not sure that distinction is
conceptually useful for the average Access user who thinks in concrete terms
of forms and reports rather than abstract terrms of instances of classes of
objects.
 
That's not technically correct. "Me" refers to the *class module* in
which the code is stored. That can be attached to a form or report,
or it can be a standalone class module.

Sure, likely a better way to state this. I trying to keep things simple, and
it likely that the concept, or understanding of what class module is not
going to help this person much.

However, "me" does not exist unless there is code. Further "me" can't be
used in the expression service. So, it is fair point that "me" is not merely
the representation of the given form, or a mere "substitution" of the forms
ref syntax as my response suggests...

Anyone can correct me..but, I don't think you can use "me" unless you have a
forms class module...
 
Allen and Albert get my vote! You have to look at the question asked and how
it's asked to get some idea of the level st which the poster is operating.
Obviously, some who asks "What does Me mean" is a newbie, and the answers
really should be geared towards that. I try to throw in a little theory when
appropriate, but not so much as to drown the original poster.

--
There's ALWAYS more than one way to skin a cat!

Answers/posts based on Access 2000

Message posted via AccessMonster.com
 
David, I understand what you mean, but I'm not sure that
distinction is conceptually useful for the average Access user who
thinks in concrete terms of forms and reports rather than abstract
terrms of instances of classes of objects.

The VBE presents all form/report modules as class objects, no? I've
always found this highly annoying, myself, as I don't want to access
them from there, but it seems to me that it exposed the underlying
reality to anyone who is writing code, that these are class objects.
 
Allen and Albert get my vote! You have to look at the question
asked and how it's asked to get some idea of the level st which
the poster is operating. Obviously, some who asks "What does Me
mean" is a newbie, and the answers really should be geared towards
that. I try to throw in a little theory when appropriate, but not
so much as to drown the original poster.

I don't see why you can't give the technical explanation and then
explicate what that means at a more practical level.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top