What is Me.[object]?

dan231

Registered User.
Local time
Yesterday, 18:09
Joined
Jan 8, 2008
Messages
158
I have seen several examples of code referencing Me.objectname.
I have never used this and can't find any info on what or why to use the Me.
 
I'm guessing it's a kind of shorthand to make the sample code more readable where you replace the word 'object' or 'objectname' with the your actual control name, say a textbox name on a form.

Make sense?
 
Hey Ken its the Me. he wants an explanation of.

There used to be a good explanation of Me. . and ! and when each could be used on the forum, but I have no idea how to find it. Me. is a reference to the current object eg a form so if you writing code for that form Me.txtbox2 would reference txtbox2 on that form.

Brian
 
My bad - I didn't read it all the way - Sorry
 
Strangly he didn't point out the misunderstanding but signed out.

Brian
 
I think there is a performance issue involved but since most of my app's don't have enough data to load them down I simply use me. because is easier and faster than typing the entire thing out - :)
 
So it just references that textbox on the same form, so then it's not really needed?

is it good practice to use it?
 
The alternative is to use Forms!formname!txtboxname
but there are differences in use I just cannot remember them.


Brian
 
I don't do either. I just reference txtboxname.
I guess that this is incorrect then? I haven't seen any problems...yet.
 
Are we at cross purposes.

If you are writing code and wish to access a txtbox on the form you cannot just say

Txtbox2 = 6

can you?

Brian
 
The alternative is to use Forms!formname!txtboxname
but there are differences in use I just cannot remember them.


Brian

I think he means he's even leaving off the forms!myform. stuff.
 
I have.

I have referenced txtbox2 = txtbox3 * 4
and the like. I have also done this to turn locked values on and off, change backgound colors, etc.
 
I see it both ways all the time so there must not be any (significant) reason either way other that making your code easier to read, consistent and hence easier to debug / maintain. I'm sure somewhere someone can come up with a millisecond difference in performance but it's a form not a 10million record query in question...

On the flip side (You had to know this was coming :)) I think fully qualifying the object (using the form.formname stuff) may make it easier to transition to other languages down the road as some are pretty anal about exact references, etc... and won't allow such shortcuts...:)
 
ok, thanks. I just was unable to find any good references on why it was used.
 
All this goes to show just how ignorant I am, perhaps its a good job that I'm retired. :eek:

Brian
 
Using the Me. reference is more useful for setting form/report properties (RecordSource, for example), but unnecessary otherwise. It's an extra identifier that can be confusing if you have more than one form open (a form/subform combination, for example). It can also lead to ambiguous references if you have controls on two different forms with the same name. For example, if you have two forms open, and both contain a field called "Primary_ID", then Me.Primary_ID can lead to unexpected results.

This is very similar to using a With/End With combination, as in this:

Code:
With Me
    .RecordSource = "MyRecordSource"
    .Caption = "MyForm"
End With

Therefore, if you have one form open called "MyForm" and you have a control on it named "MyTextbox", all of these are identical:

MyTextbox = "test"
Me.MyTextbox = "test"
Forms("MyForm").Form.MyTextbox = "test"

The best practice, IMO, is to avoid Me. when it's not necessary as it's just extra coding. You do need it (or the Forms("MyForm").Form example from above) for setting formwide variables (RecordSource, Caption, etc.), but for individual controls, it's overkill.
 
There are different ways to fully qualify an object

Explicit

Forms!formname!controlname

Sub Routine

Me!controlName or
MyObject = CodeContextObject
MyObject!controlname

Function

With CodeContextObject
.controlname

I prefer Functions as any reoccurance the Stock Field can be reference as .[Stock]

You have to be explicit when referring to another form or report other than the current one with the focus.

Simon
 

Users who are viewing this thread

Back
Top Bottom