Me. vs Me!

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

Guest

Good morning
I know I saw something in the last few days on this topic, but I lost it. I continue to have to reference form controls like Forms!frmFORMNAME.controlname rather than (from form code) Me!controlname or Me.controlname

I did some hunting in the help files and they suggest that the Me! or Me. syntax may only be used in a class module (which I don't believe this qualifies as).

Can someone please help me out
Thank you
Derek
 
Derek Wittman said:
Good morning,
I know I saw something in the last few days on this topic, but I lost it.
I continue to have to reference form controls like
Forms!frmFORMNAME.controlname rather than (from form code) Me!controlname
or Me.controlname.
I did some hunting in the help files and they suggest that the Me! or Me.
syntax may only be used in a class module (which I don't believe this
qualifies as).
Can someone please help me out?
Thank you!
Derek

Sounds like the form might be corrupted. Do all your forms do this or just
the one?

BTW a form _is_ a class object.
 
Derek,

First and foremost forms as fully qualified classes.

I use the bang symbol "!" only when using the implicit objects such as
[Forms], [Reports] etc which represent a collection of other objects in the
property inspectors.

You use the Me. syntax when refering to a property, subroutine or function
from within the class module.

Regards,
Dan




Derek Wittman said:
Good morning,
I know I saw something in the last few days on this topic, but I lost it.
I continue to have to reference form controls like
Forms!frmFORMNAME.controlname rather than (from form code) Me!controlname or
Me.controlname.
I did some hunting in the help files and they suggest that the Me! or Me.
syntax may only be used in a class module (which I don't believe this
qualifies as).
 
Derek,

Me always refers to the class period. Since your form is a class when ever
you refer to me you are point to the class. So if you have a control on
your form you would refer to the value of the control like this:

Me.MyControl.Value = "jfkldfj"

When you execute me.Refresh you are asking the form to refresh its
underlying datasource. So if you have controls that are based on other
controls your syntax would look something like this:

Private Sub cmb1_AfterUpdate()
me.cmb2.Value = me.cmb1.Value
End Sub


Derek Wittman said:
Thanks, Dan. I have a small handful combo boxes that are based on one
another's values, so I use Me.Refresh in my AfterUpdate event procedures a
lot.
But I can never get the Me. syntax to successfully represent a form control's value...

Thanks!
Derek

----- solex wrote: -----

Derek,

First and foremost forms as fully qualified classes.

I use the bang symbol "!" only when using the implicit objects such as
[Forms], [Reports] etc which represent a collection of other objects in the
property inspectors.

You use the Me. syntax when refering to a property, subroutine or function
from within the class module.

Regards,
Dan




Good morning,
I know I saw something in the last few days on this topic, but I
lost it.
I continue to have to reference form controls like
Forms!frmFORMNAME.controlname rather than (from form code) Me!controlname or
Me.controlname. or Me.
syntax may only be used in a class module (which I don't believe this
qualifies as).
Thank you!
Derek
 
Derek,

You are correct!

Dan


Derek Wittman said:
Dan,
This is great information. Very helpful. So, if I use me.ctrlname, I
need to make sure I use me.ctrlname.value (the property), if I understand
correctly. Of course, I could always set a variable to be equal to
me.ctrlname or me.ctrlname.value, right?
Thanks!
Derek

----- solex wrote: -----

Derek,

Me always refers to the class period. Since your form is a class when ever
you refer to me you are point to the class. So if you have a control on
your form you would refer to the value of the control like this:

Me.MyControl.Value = "jfkldfj"

When you execute me.Refresh you are asking the form to refresh its
underlying datasource. So if you have controls that are based on other
controls your syntax would look something like this:

Private Sub cmb1_AfterUpdate()
me.cmb2.Value = me.cmb1.Value
End Sub


Thanks, Dan. I have a small handful combo boxes that are based on
one
another's values, so I use Me.Refresh in my AfterUpdate event procedures a
lot. form
control's value... objects such
as
[Forms], [Reports] etc which represent a collection of other
objects
in the
property inspectors.
subroutine or
function
from within the class module.
wrote in
message
lost it.
I continue to have to reference form controls like
Forms!frmFORMNAME.controlname rather than (from form code) Me!controlname or
Me.controlname.
Me!
or Me.
syntax may only be used in a class module (which I don't believe this
qualifies as).
 
Derek Wittman said:
Thanks, Rick. Generally all of them do. I've given up on the Me syntax
some time ago - and I'm finally tired of typing the whole thing...
How does what appears to be a working form get corrupted? Any way I can
fix it without recreating it?

If this has been an on-going problem for you it suggests that you are
simply doing something wrong in your syntax usage. I have never heard of
any problems with using Me.

Do you have some specific syntax examples that don't work? As a simple
test you could drop a command button on any form and put the following in
the Click event...

MsgBox Me.Name

The above code should produce a Message Box with the name of the form
displayed. Does that work?
 
Derek Wittman said:
Rick,
Yes, that works. Referring to my last reply to Dan (Solex), I've come to
learn that Me by itself is nothing. Nor is Me.controlname. It seems that
when I look for a result, I need to consider the property... Name, value,
visible, etc.

Actually Me.ControlName should be the same as Me.ControlName.Value since
Value is the default property of a control or field. The only time I have
ever explicitly used .Value id when looking at a TabControl which (for some
reason) seems to require it in Access 97.
 
Derek,

Your not completely correct, Me or Me.ControlName by itself is something.
For instance if you create a function that disables a control you would use
Me or Me.ControlName by itself in addition if the control have a default
property you could use Me.ControlName = "X"

Call DisableControl(Me.Control)


Sub DisableControl(ByRef ctl As Control)
ctl.Disabled = True
End Sub

Derek Wittman said:
Rick,
Yes, that works. Referring to my last reply to Dan (Solex), I've come to
learn that Me by itself is nothing. Nor is Me.controlname. It seems that
when I look for a result, I need to consider the property... Name, value,
visible, etc.
 
Hi Derek,

I believe Dan and Rick have answered your direct
question, but, if you are interested, there are a lot of
old posts on the related issue of
theadvantages/disadvantages of using the ! (bang) vs
the . (dot).

If you are interested, try a search on google such as the
following (watch wrapping):

http://groups.google.com/groups?hl=en&lr=&ie=UTF-
8&q=bang+vs+dot&btnG=Search&meta=group%
3Dmicrosoft.public.access.*

Some of the posts are quite interesting and informative.

-Ted Allen
-----Original Message-----
Good morning,
I know I saw something in the last few days on this
topic, but I lost it. I continue to have to reference
form controls like Forms!frmFORMNAME.controlname rather
than (from form code) Me!controlname or Me.controlname.
I did some hunting in the help files and they suggest
that the Me! or Me. syntax may only be used in a class
module (which I don't believe this qualifies as).
 
Thank you, Ted. Yes, I am certainly interested in checking out the old DOT vs BANG posts. I don't access the NGs through Google, but rather through the Microsoft Communities page itself. I'll have to give Google a try

I appreciate the follow-up
Dere

----- Ted Allen wrote: ----

Hi Derek

I believe Dan and Rick have answered your direct
question, but, if you are interested, there are a lot of
old posts on the related issue of
theadvantages/disadvantages of using the ! (bang) vs
the . (dot)

If you are interested, try a search on google such as the
following (watch wrapping)

http://groups.google.com/groups?hl=en&lr=&ie=UTF
8&q=bang+vs+dot&btnG=Search&meta=group
3Dmicrosoft.public.access.

Some of the posts are quite interesting and informative

-Ted Alle
-----Original Message----
Good morning
I know I saw something in the last few days on this
topic, but I lost it. I continue to have to reference
form controls like Forms!frmFORMNAME.controlname rather
than (from form code) Me!controlname or Me.controlnamethat the Me! or Me. syntax may only be used in a class
module (which I don't believe this qualifies as).
 
My pleasure. I actually use the Microsoft Communities
website for posting as well, but I find the search
capabilities not very good. I generally use Google to
search the communities for various topics, and also to
check for new posts with my name in the thread to try to
avoid missing responses.

-Ted Allen
-----Original Message-----
Thank you, Ted. Yes, I am certainly interested in
checking out the old DOT vs BANG posts. I don't access
the NGs through Google, but rather through the Microsoft
Communities page itself. I'll have to give Google a try.
 
Derek Wittman said:
Good morning,
I know I saw something in the last few days on this topic, but I lost
it. I continue to have to reference form controls like
Forms!frmFORMNAME.controlname rather than (from form code)
Me!controlname or Me.controlname.

I did some hunting in the help files and they suggest that the Me! or
Me. syntax may only be used in a class module (which I don't believe
this qualifies as).

Can someone please help me out?
Thank you!
Derek

Derek -

I looked through the earlier posts in this thread, and I'm not sure that
they made it clear that you can only use the "Me" keyword from VBA code
that is part of the form's (or report's) code module. That is, in code
"behind the form", you can use Me.controlname or Me!controlname to refer
to a control on that form, or Me.propertyname to refer to a property of
that form, but ...

(a) you can't use "Me" to refer to a form other than the one the
code is running on, and

(b) you can't use "Me" in a standard module; only in a class module
(a form or report's code module, or a user-defined class module), and

(c) you can't use "Me" in a controlsource expression or in a query.

If you are concerned with two forms, "FormA" and "FormB", then in code
running behind FormA you can refer to control txtFieldOnA on FormA using
any of these:

Me.txtFieldOnA
Me!txtFieldOnA
Forms!FormA!txtFieldOnA

(That last isn't as efficient as the others, though.) If there's a
control on *FormB* named txtFieldOnB, though, the only way you can refer
to it from code on FormA is

Forms!FormB!txtFieldOnB

or one of several equivalent versions of that reference syntax. You
can't use "Me", though, because txtFieldOnB isn't on "me" (FormA); it's
on FormB.

Does that explanation help at all?
 
It is worth noting that using Me keyword you can access only public members
of a class. Since controls placed on a form are the public members by
default, you can use this syntax, but it's better to omit the Me keyword at
all.

Me.Text.Value = Text.Value.

IMO Me adds an extra layer of indirection to a call of a method or a
property. Perhaps a compiler's optimizer can take proper care of it, but I
am not sure if it really does.

Me is most useful when you need to pass a reference of an instance of a
class itself to some external procedure or assign it to a variable like

Result = DoSomethingWithMe(Me) 'or
Set AClassVar = Me

Also it is useful as a way to fire the IntelliSense - If you forget a name
of a class member, type me. and you will be presented a list of all public
members of a class. However it won't work at all with private members of the
class, for example:

Public A_Var as Integer ' Visible in IntelliSense
Private Another_Var as Integer ' Not visible in IntelliSense

Sub Test()
Me.A_Var = 100 ' OK
'Me.Another_Var = 200 ' Compiler Error: Method or datamember not found
'...
A_Var = 300 ' Reassignment, OK
Another_Var = 400 ' Without Me compiler is happy.
End Sub
 
I've come to learn that Me by itself is nothing. Nor is Me.controlname.

This is very much not true. Each of these expressions refers to an object,
and you can do anything you like with them that you can do with objects:

Set frmMyNewForm = Me

If ControlIsBlue(Me!txtFullName) Then

collTickBoxes.Add Me.Controls("chkSession" & n), Format(n,"00")

DoCmd.Close acForm, Me.Name

and so on. Quite often the Me is technically redundant, but really helps
readability and is thus rarely wasted. In the same vein, relying on default
properties is a dangerous practice: I generally use

If Me.Controls("chkUseLiteral").Value = True Then

rather than

If chkUseLiteral Then

because it screams at me if I ever change the name of the control, or alter
it to an option group etc, whilst it is easy to overlook the second one
thinking it's a variable name. Don't forget that it only takes a few
seconds to write a line of code -- but it can take hours to read it again
later... :-)

All the best


Tim F
 
Back
Top