tags:

views:

117

answers:

3

Hello

I have a form module in my access project. In the top of the module, I declare a variable like so:

option explicit

private id_foo as long

I want to explicitely state that I need the variable in my form module by using the private access modifier on it.

Now, further down in the same form module, I have a function that needs to know and/or modify the value of id_foo:

function bar() as long 
  call do_something(me.id_foo)
end function

Yet, this doesn't work. But when I change the private modifier to a public modifer like

public id_foo as long

it does work.

This behaviour strikes me as odd or unintuitive, and, in fact, I can't see the meaning of public and private if I have to declare the variable as public anyway in order to use it in the same form module.

So, am I overlooking something obvious or is this how it is supposed to be?

Thanks / Rene

+2  A: 

Try it without the "me" in front of id_foo:

function bar() as long  
  call do_something(id_foo)  
end function
hawbsl
+1  A: 

If you use the me keyword, you can see only public members, properties (also Form and VBA).

Andreas Hoffmann
Good Answer. Not always perfect, but the intellisense would not show Me.id_foo unless it was public.
Jeff O
This is standard behavior for class modules. One is likely not to realize it until one has used a standalone class module, but ME refers to the CLASS MODULE, not the form/report it's attached to.
David-W-Fenton
A: 

A form along with its module actually represents a class object. You can also create (instanciate) multiple Instances of that class object.

So, Any variable you declare as public becomes a public property of that class object. Note that any function in the forms code module declared as public becomes a public method of that class object. All of these properties and methods then show up in the intel-sense when you type in the "me." keyword.

If you declare the variable as private, then that variable (or function) will not be exposed as a public property (variable) or a public method (function) of the form.

So the simple solution your cases is to drop the use of the me keyword in your code, and it you code will run just fine at.

So declaring as public or private does have an effect here. In fact, “private” is the default.

So, public will expose the variable and/or functions as properties and methods of that form which is a class object (note that you can have Multiple instances of the same form loaded at the same time).

If you decleare things as private (the default, so you don’t have to do anything for the Variable or function) then you can still use the value in ANY code routine in that forms code module, but it will not be public exposed as a property/method and thus you can't use me.

Thus, your code will work fine if you remove the use of the me., and just go:

function bar() as long 
   call do_something(id_foo)
end function
Albert D. Kallal
Thank you for your explanation. So, `me` is not exactly the same as `self` or `this` as found in other languages.
René Nyffenegger