Scope of Variant / Array

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

Guest

Hi,

I've got a form with a linked subform. In the subform I have references to
variables declared 'public' in the parent form, e.g.:

public m_locked as boolean

I'm successful in referencing those variables in the subform by using a
'parent.' prefix, e.g.:

if parent.m_locked then...

Similarly, I have an array declared public to the parent which I populated
from a recordset prior, yet when I attempt to reference an element in it
(e.g. parent.m_setting(1,0) ), I get an error:

run-time error '451':

property let procedure not defined and property get proceduer did not return
an object

I'm able to get an accurate response when I type "?ubound(m_setting)" from
the Immediate Window while I'm debugging a line in the subform, so doesn't
that mean it's in scope? Yet I can't get a returned value on any element in
it.

Why can't I get the values out? What am I doing wrong?
 
Hi -

Since in the immediate window you can reference the array without the parent.
qualifier i.e. ?ubound(m_setting) , your code should work without the parent.
qualifier as well. Try removing the parent. from the array reference.

John
 
I'm so sorry, I meant to write "?ubound(parent.m_setting)" :(

Without the parent reference I get the expected 13/type mismatch.
 
I'm so sorry, I meant to write "?ubound(parent.m_setting)" :(

Without the parent reference I get the expected 13/type mismatch.
 
Hi -

Even though you declared the array "public" in the main (parent) form, it
isn't really public - it is known only to the procedures within the parent
form's module, which means you cannot reference it in the sub-form's code.
As well, the parent. property cannot reference variables, only properties of
the parent.

To make a variable truly public, you must declare it as public in a standard
code module.

John

I'm so sorry, I meant to write "?ubound(parent.m_setting)" :(

Without the parent reference I get the expected 13/type mismatch.
[quoted text clipped - 31 lines]
 
Ok, but is this unique to arrays?

I ask because I have no problem with the other variable, m_locked from my
example.

I'm using multiple instances of the same form, so I need to try and get this
variable to be specific to my form for when users switch between instances
(rather than in a code module). If you've got another idea for this I'm all
eyes.

J_Goddard via AccessMonster.com said:
Hi -

Even though you declared the array "public" in the main (parent) form, it
isn't really public - it is known only to the procedures within the parent
form's module, which means you cannot reference it in the sub-form's code.
As well, the parent. property cannot reference variables, only properties of
the parent.

To make a variable truly public, you must declare it as public in a standard
code module.

John

I'm so sorry, I meant to write "?ubound(parent.m_setting)" :(

Without the parent reference I get the expected 13/type mismatch.
[quoted text clipped - 31 lines]
Why can't I get the values out? What am I doing wrong?
 
J_Goddard via AccessMonster.com said:
Hi -

Even though you declared the array "public" in the main (parent) form, it
isn't really public - it is known only to the procedures within the parent
form's module, which means you cannot reference it in the sub-form's code.
As well, the parent. property cannot reference variables, only properties
of
the parent.

No, not true at all. note how KitCaz said it works just fine with variables.

Remember, a form code module is the same as a class module.

thus,

public m_setting as string


is a legal var def, and any routine can reference the above value by
supplying the forms reference, and then the var name
(assuming the form is open).

In fact, any public variable declared in a class module (including forms)
simply becomes a settable property of the form.

However, the following is NOT legal


public buf(10) as string

If kitCaz had actually tried to compile the above line of code, you see the
following error message:

---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Constants, fixed-length strings, arrays, user-defined types and Declare
statements not allowed as Public members of object modules
---------------------------
OK Help
---------------------------


(I did a control-c while viewing the dialog box to copy the above).

The simply solution is to simply declare the array as private, and then
build pubic propriety for to expose the array
in the forms code.

The following will give programming access to the array.

Option Compare Database
Option Explicit

Private vbuf() As Variant

Public Property Let myVbuf(i As Integer, str As String)


vbuf(i) = str


End Property

Public Property Get myVbuf(i As Integer) As String

myVbuf = vbuf(i)

End Property


Now, in code, you can go:

me.parent.myVbuf(3) = "hello"

msgbox "3rd value should be hello...here it is " & myvbuf(3)

So, you *can* reverence any variable declared public in a form, you just
have to supply the forms qualifier..

me.MyVariblename
or

forms!NameOfForm.MyVariableName = "hello"

or
me.Parent.MyVariableName = "hello"

Since "me" is the default, then you also use:

MyVariablename

All are above are valid.. In fact, if you have form named zoo, then if you
reference the BASE class object name of that form, inteli-sense will
actually show the above public members (variables), and also the public
property we added as a drop down list.

In code, I often start typing the follwining

msgbox forms!Zoo.varname

(and, then I goo...hum, forgot the name..I don't want to go and look it
up..so, I type in:

forms_Zoo.

----------^ ..the instaant I hit the "dot", then inti-sense will popup a
list of varilbes and properites ad public functions. I can view, or even
select the varible name...I cut otu the whole line, and the continue typeing
my orignal line of code:

msgbox forms!Zoo.MyVar.......

To make a variable truly public, you must declare it as public in a
standard
code module.

Truly pubic, yes, but as a long as the form is open, that public var in that
form is useable by ANY code and any form, or any thing in the appcation, you
just have to provide a forms qualifier.

In the posters example, he does not need a global variable, but simply to
reference a variable that is part of parents form code. Further, if KitCaz
continues to code this way, then you can even have multiple instances of the
SAME form opened..and the variables will REMAIN separate from each other.
(just like a class object would.. I you use global here , they will trip
over each over, and you can't have multiple instances of the form function
correct if you use global -- they should be avoided here);

So, I *do* in fact recommend using public variables in a forms code module
for his case.

So, the ONLY limitation here is that arrays are NOT allowed to be public in
class object, and that also applies to a forms code module which is the same
as a class object...

However, as shower, simply adding a public property to the class modules
code, you can use nearly the same syntax, and you can expose the private
array by declaring a public property.
 
Thank you for set setting me straight on this one - much appreciated. I can
put this to good use.

John


[quoted text clipped - 4 lines]
of
the parent.

No, not true at all. note how KitCaz said it works just fine with variables.

Remember, a form code module is the same as a class module.

thus,

public m_setting as string

is a legal var def, and any routine can reference the above value by
supplying the forms reference, and then the var name
(assuming the form is open).

In fact, any public variable declared in a class module (including forms)
simply becomes a settable property of the form.

However, the following is NOT legal

public buf(10) as string

If kitCaz had actually tried to compile the above line of code, you see the
following error message:

---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Constants, fixed-length strings, arrays, user-defined types and Declare
statements not allowed as Public members of object modules
---------------------------
OK Help
---------------------------

(I did a control-c while viewing the dialog box to copy the above).

The simply solution is to simply declare the array as private, and then
build pubic propriety for to expose the array
in the forms code.

The following will give programming access to the array.

Option Compare Database
Option Explicit

Private vbuf() As Variant

Public Property Let myVbuf(i As Integer, str As String)

vbuf(i) = str

End Property

Public Property Get myVbuf(i As Integer) As String

myVbuf = vbuf(i)

End Property

Now, in code, you can go:

me.parent.myVbuf(3) = "hello"

msgbox "3rd value should be hello...here it is " & myvbuf(3)

So, you *can* reverence any variable declared public in a form, you just
have to supply the forms qualifier..

me.MyVariblename
or

forms!NameOfForm.MyVariableName = "hello"

or
me.Parent.MyVariableName = "hello"

Since "me" is the default, then you also use:

MyVariablename

All are above are valid.. In fact, if you have form named zoo, then if you
reference the BASE class object name of that form, inteli-sense will
actually show the above public members (variables), and also the public
property we added as a drop down list.

In code, I often start typing the follwining

msgbox forms!Zoo.varname

(and, then I goo...hum, forgot the name..I don't want to go and look it
up..so, I type in:

forms_Zoo.

----------^ ..the instaant I hit the "dot", then inti-sense will popup a
list of varilbes and properites ad public functions. I can view, or even
select the varible name...I cut otu the whole line, and the continue typeing
my orignal line of code:

msgbox forms!Zoo.MyVar.......
To make a variable truly public, you must declare it as public in a
standard
code module.

Truly pubic, yes, but as a long as the form is open, that public var in that
form is useable by ANY code and any form, or any thing in the appcation, you
just have to provide a forms qualifier.

In the posters example, he does not need a global variable, but simply to
reference a variable that is part of parents form code. Further, if KitCaz
continues to code this way, then you can even have multiple instances of the
SAME form opened..and the variables will REMAIN separate from each other.
(just like a class object would.. I you use global here , they will trip
over each over, and you can't have multiple instances of the form function
correct if you use global -- they should be avoided here);

So, I *do* in fact recommend using public variables in a forms code module
for his case.

So, the ONLY limitation here is that arrays are NOT allowed to be public in
class object, and that also applies to a forms code module which is the same
as a class object...

However, as shower, simply adding a public property to the class modules
code, you can use nearly the same syntax, and you can expose the private
array by declaring a public property.
 
Back
Top