What is the meaning & use of "& ME!"?

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

Guest

In the VB section I find this combination constantly but have not been able
to find it in the documentation so I can use it properly.
 
"&" is a concatenater for strings. For example, if I do the following

strFirst = "The first part"
strSecond = " the second part."
strConcatenated = strFirst & strSecond

Then the value in strConcatenated will be

The first part the second part.

"Me" is shorthand for the form or report that the code is running on. If you
have a form open called Form1 you can refer to that form by the code
Forms!Form1. However, if the code is on Form1, you can shorten the previous
to just Me. Me only works in VBA, it doesn't work in the Control Source of
calculated controls.

The "!" after the Me is just a separator between components making up the
path to the object being referred to. Just as you would use "\" to separate
the path to a file (i.e. C:\My Documents\MyDoc.doc). There are two operators
you can use, the "!" and the ".". You can usually get by with the "." and it
has the advantage of enabling the Intellisense to prompt you with possible
next arguments or objects.
 
Hi Ashton,

The "Me" keyword is a self-identifier for referencing a property or value
within the forms own class module (the code behind the form). This allows
you to set or get values while ensuring that you don't have naming collisions
in those cases where you may have multiple forms open that have the same
variable names being used.

For example, if you want to set the value of a textbox something, you would
use:

Me.txtUserName = "Bob"

This will place the name Bob inside the textbox that is on your form. It
can be viewed as a shortcut to void having to type:

Forms!frmCustomerInformation!txtUserName = "Bob"

Personally, I find that using Me makes it a bit easier to read than the later.

And lastly, if you were to use the later example and fully namespaced
everything and then later decided that you wanted to change the name of the
form or the txtUserName field to something else, you would effectively break
your code everywhere you used it, but the Me keyword ensures that the proper
object is referenced no matter what you do (outside of changing the name of
the field). This makes debugging a lot easier.

Hope this helps a bit.

Lance
 
Back
Top