What does Me. mean?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm new to using VBA and have a question. What does Me. mean when used in
code for forms? I've seen that in several postings, but have no clue what it
means.
 
"Me" stand for the current form where the code (Me) is located on.

So instead of writing the full path
Forms![FormName]![FieldName]

you can write
Me.[FieldName]

As long that this code is located in the FormName and there is a field
called FieldName
 
btee said:
I'm new to using VBA and have a question. What does Me. mean when used in
code for forms? I've seen that in several postings, but have no clue what it
means.

It refers to the current form. If you have a control named MyControl on the active
form,
then Me!MyControl (or not as nicely, Me.MyControl) refers to that control. Why I use
the bang (!) rather than the dot(.) is because the bang really should be used to
refer to
control names and the dot to properties. Me!MyControl.Text would refer to the
contents
of text box MyControl on the current form. Some people argue that control names ARE
properties of the form but I find it just leads to confusion. Also, if you used a
reserved
word such as Name for a control name by mistake (a bad thing to do), Me.Name would
give an error whereas Me!Name would not.

Tom Lake
 
Tom,

1) Last I heard/read, the ! syntax is being deprecated in future versions.
2) the "Me" object actually referrs to the current instance of the open form
(it is possible to have multiple instances of the same form open
simultaneously)

Thus, in code, forms!MyOpenForm!MyControl could be an ambiguous reference if
multiple instances of a form are open simultaneously (this is a problem for
queries too - if a query driving a combobox list has forms!myform!myControl
as a Where conditional expression, which instance of the "myform" form and
which copy of the control value will the query return when it executes?)

Within form VBA code, this potential ambiguty is eliminated by the use of
"Me."

Thus, my preference for "me.controls("myControl").value"-style syntax is
more bulletproof. Yes, it is more verbose, but this helps in another way: the
code is more self-documenting, so when you have to tweak something you
haven't looked at for 6+ months when you open it up, the code meaning is
easy(/easier) to discern (even for a different person). The terseness of the
!-style syntax works against code readability and maintainability IMO.

Also, with the .-style syntax allows for more programming flexibilty and
increased data-drivability of your code. Consider that:
me.controls(strSomeControlNameVariableHere).value
allows you to programatically determine which control you are referencing at
run-time. Try that with your
"me!FixedControlNameHere.value"-syntax.

Oh...and Me.Name will return the name of the form as a string (...which it
is also the reason it is a bad idea to use "Name" as either Field or Control
names), but you knew that, right? ;-)
 
Back
Top