"Me" variables

  • Thread starter Thread starter ekg
  • Start date Start date
E

ekg

Guest
I have what should be a simple question for a seasoned MS Access VBA Developer to answer.

Where are "Me" variables defined? And how does one define new variables of this type? I am modifying some existing VBA code within a database, and simply cannot find where some Me variables are defined. The Me variables appear within the code of the Event Procedure for a button used to generate a report.

Any help is much appreciated. I am a seasoned OO developer, but have little experience with VBA or VB. Wrox's "Beginning Access 2000 VBA only briefly touches on the Me property, and I have had no success searching the web for related information.

Thank you.
 
Me.Something usually refers to a control on a form (or whatever the code is for). Such as MyForm.MyTextBox. Me is just a short way of referring to the form. Is that what you're looking for?
 
ekg,

When you are in VBA code for a form (or report), the ME
variables are the objects defined on that form (or report)
as well as the ones that Access will provide you in the
context of your form (or report).

If you want to refer to a bound textbox named HoursWorked
when you type the "Me." you will get a dropdown list. When
you type the "H", the list will be further refined.

If you do not see your control on the list, then it is not on
your form.

Wayne
 
The Me operator is an object reference (implicit) used to signify either a class or a form and is used within its class or form.
 
Thanks for all of your responses.

Perhaps I need to rephrase my question.

I understand to what "Me" refers, but do not understand where current variables are defined, or where to define new variables.

So, for example, if I am looking at VBA code for a form, and type Me., a drop down list of variables appears which are within scope of the form. If txtEKG is listed in the drop down list, for example, I am obviously able to use the variable.

What I am wondering is how I can add new variables to the form's scope so that they appear on that drop down list. I am also wondering where to find the definitions of the variables that already currently exist.

So if I look at the VBA code for a form, and the code reads:
Me.txtEKG = ""
where do I look for the definition of txtEKG? And where would I define a new variable, such as txtEKG2? If I simply enter a new line which reads:
Me.txtEKG2 = ""
an error is returned because txtEKG2 needs to be defined somewhere.

This is probably a very simple issue, but I just can't seem to find these definitions, no matter where I look. Thanks for your patience.

Hopefully this makes sense. Thanks.
 
ekg,

Default in the listbox appear al properties and methods of the form. The variable txtEKG is an textbox on your form and from VBA you can handle the value (and much more, try typing . after txtEKG and you got another listbox) of that textbox.

You can add variables by declaring them as Public in the form's class-module at module-level:
Code:
Option Compare Database
Option Explicit

Public sMyVar As String

Private Sub txtEKG_AfterUpdate()
  sMyVar = Me.txtEKG
End Sub
This is something to play with...

Bert
 
The fields that you see when you use Me. are members of the form's fields collection. These are the controls on the form and also the fields of the form's recordsource. When a wizard makes the form, it assigns the controlsource as the control name. So if you had a field called CustID the wizard would build a control and assign to it the name, CustID. You would see only one entry for CustID in the intellisense list and code references to CustID would be assumed to be to the control rather than to the field of the controlsourse. If you are going to be writing any VBA at all, it is a good idea to rename the controls so that they have distinct names. I prefix mine with "txt" so on my forms the control name would be txtCustID. That way I can be specific when I write code so there is no confusion as to which object my code refers. If you do that, you will see both names in the intellisense list - CustID and txtCustID.

You could add to the intellisense list by adding a new control to the form or by adding a new column to the form's recordsource. Public and private variables are dimensioned as with normal modules and they are NOT referenced with the Me. qualifier because they are not part of any form collection.
 
Pat Hartman said:
Public and private variables are dimensioned as with normal modules and they are NOT referenced with the Me. qualifier because they are not part of any form collection.

This is right for Standard modules, but when you declare a Public variable at Form-module-level, it is used as property of the form and so it comes back into the intellisense list. Private variables don't come back there.
Keep in mind that the value of the variable only is hold while the form is open (it's an instance of the forms' class), so when you closed the form, hou can't use the variable.
 

Users who are viewing this thread

Back
Top Bottom