What is the significance of 'Me'?

  • Thread starter Thread starter Noel Sant
  • Start date Start date
N

Noel Sant

In coding examples in a number of Access VBA books (especially those by
Helen Feddema, but I'm sure I've seen it elsewhere), the word 'Me' is used,
but not explained anywhere that I can see. As in:

Me.Requery, or
Me![cboSalesperson] = Null

It seems to be something to do with the current form, is the best I can
guess. Can anyone explain properly, please?

Many thanks,

Noel Sant
 
Noel said:
In coding examples in a number of Access VBA books (especially those
by Helen Feddema, but I'm sure I've seen it elsewhere), the word 'Me'
is used, but not explained anywhere that I can see. As in:

Me.Requery, or
Me![cboSalesperson] = Null

It seems to be something to do with the current form, is the best I
can guess. Can anyone explain properly, please?

It is the current "container". If the code is in a form's module then it
refers to the form. If in a report's module then it refers to the report.
If in a class module then it refers to the current instance of the class.
 
It seems to be something to do with the current form, is the best I can
guess. Can anyone explain properly, please?

Yes, you got it right.

Since so much code deals with the current forms events, then it would much
painful to go

forms!MyFormName!MyFieldName

So, the first part

forms!MyFormName

Can be replaced with

me

This also tends to make code more modular.

You can go for example

Call MyCoolSub(me)

in place of

Call MyCoolSub(forms!MyFormName)

and, the sub could look like

Public Sub MyCoolSub(frm as form)

frm.Requery

end sub

The above sub is an example, and a lame one at that..but you can see how the
code would do a form requery....
 
Hi,
"me" reffers to current class instance, where code running, if code running
in form or report - then to form's or report's object
 
Noel Sant said:
In coding examples in a number of Access VBA books (especially those
by Helen Feddema, but I'm sure I've seen it elsewhere), the word 'Me'
is used, but not explained anywhere that I can see. As in:

Me.Requery, or
Me![cboSalesperson] = Null

It seems to be something to do with the current form, is the best I
can guess. Can anyone explain properly, please?

The keyword "Me" is an explicit reference to the object in which the
code is running. So for code running behind a form, it's a reference to
that form; for code running in a report, it's a reference to that
report; for code running in a class module, it's a reference to the
specific instance of that class module where the code is being executed.
One uses it in referring to properties or methods of the object, or (as
in the case of "Me!cboSalesPerson") to members of a collection belonging
to that object. It's not always necessary to use the "Me" keyword,
since VBA's method of resolving names used in code will usually figure
out the correct object reference without it; however, there are times
when it won't, so it's best to make sure by using "Me" when appropriate.
Also, doing that will ensure the most efficient resolution of the method
or property name.
 
Let's take it just a little further.

In your first example Me.Requery
1) Me is referring to the form itself
2) the dot is indicating the next thing is a PROPERTY or ACTION of the form
itself
3) and then Requery is the action you want performed on that form

in your second example Me![cboSalesPerson]
1) Me is referring to the form or report
2) the bang (!) is indicating that what follows is an OBJECT on that form or
report
3) [cboSalesPerson] is the name of the object on that form or report you are
referring to
 
Many thanks for replies about the 'Me' keyword. It all makes perfect sense
now.

Regards,

Noel
 
Of course to understand the true significance of 'Me', you may want to
try out transcendental meditation mixed with yoga and maybe a trip to a
good Irish pub or an even better Taverna.

:)
David H
(I have been waiting patientily for you to get to your answer because
I've been dying to say that.)
 
If you have both a field (from the datasource) and a control with the
same name, the field will be addressable from the bang notation, and the
control from the dot notation.

If you haven't yet, you *will* confuse these one day. Believe me. :-)

I usually rename my controls so there is no name confusion possible.
Sometimes it shows as #Error
Let's take it just a little further.

In your first example Me.Requery
1) Me is referring to the form itself
2) the dot is indicating the next thing is a PROPERTY or ACTION of the form
itself
3) and then Requery is the action you want performed on that form

in your second example Me![cboSalesPerson]
1) Me is referring to the form or report
2) the bang (!) is indicating that what follows is an OBJECT on that form or
report
3) [cboSalesPerson] is the name of the object on that form or report you are
referring to





:

Yes, you got it right.

Since so much code deals with the current forms events, then it would much
painful to go

forms!MyFormName!MyFieldName

So, the first part

forms!MyFormName

Can be replaced with

me

This also tends to make code more modular.

You can go for example

Call MyCoolSub(me)

in place of

Call MyCoolSub(forms!MyFormName)

and, the sub could look like

Public Sub MyCoolSub(frm as form)

frm.Requery

end sub

The above sub is an example, and a lame one at that..but you can see how the
code would do a form requery....


--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
(e-mail address removed)
http://www.members.shaw.ca/AlbertKallal
 
Back
Top