Reference variables Dim'd in the parent?

  • Thread starter Thread starter Bill Stanton
  • Start date Start date
B

Bill Stanton

How does the code sheet of a sub-form
reference variables Dim'd in the parent?
I.e., syntax similar to referencing a
control in the parent form?


Thanks,
Bill
 
If you're looking for something like
Me.Parent.VariableName

then no can do.

Only way to reference a variable in the parent form is to have it declared
as a public variable and then you can use it in the subform's code *so long
as the parent form is open*.
 
How does the code sheet of a sub-form
reference variables Dim'd in the parent?

You can't. If you need to share variables among multiple forms/reports, then dim
them in a standard module (a global module, not one behind a form or report).
 
And to add to what I just posted....you must declare the variable as public
in the form's declarations section, not within a procedure in the form's
module.
 
Ken,
That's what I tried to begin with, as I'm used to doing
that via general modules.
Bill
 
And to add to what I just posted....you must declare the variable as public
in the form's declarations section, not within a procedure in the form's
module.

Ouch! Ken is right and I am wrong!

I always knew you could reference procedures and functions declared as public
from outside of the container form, but I didn't realize that it extended to
variables.
 
I always knew you could reference procedures and functions declared as public
from outside of the container form, but I didn't realize that it extended to
variables.

Incidentally, after dimming my public variable in the main form's declaration
section of its module:

Public intMyNumber As Integer

.... and setting its value in the main form's "On Open" event ...

intMyNumber = 1234

.... I was able to retrieve the variable's value from within a procedure in the
subform's module in the following manner:

MsgBox "The variable 'intMyNumber' in the parent form contains the value: " _
& Me.Parent.intMyNumber

Hoping that this might help clear things up for Bill. Now that it's cleared up
for me. <g>

:-)
 
I checked several times and everything is as Ken
suggested, but I continue to get error messages
that the variable is not defined.

Any more thoughts before I move them to a
general module?

Bill
 
Hum...they should work.


Option Compare Database
Option Explicit

Public strMyTest as string


In your sub-form, you can then go:


me.Parent.strMyTest = "hello"


Also, any function you declare in the form as public actually becomes a
"method" of the form. And is also very useful. I often have code in a
sub-form that must run code in the parent form. I will use things like:


Me.Parent.MyRefresh

The above would run my custom re-fresh code in the parent form. (a public
function called MyRefresh).

You can even use the class object defs of Property let, and Property get for
the form.
 
Ahaaa! There is a difference then between
referencing global variables Dim'd Public
in modules versus those we've been discussing
in this thread. I DID NOT use the form
"Me.ParentName.VariableName". Rather,
I tried to use just "VariableName" unqualified.

All is once again well in the Sierras.

Thanks,
Bill
 
Hi Albert!

I've saved your reply for future reference. I've got
several instances where I'd like to have code in
the parent form that I'd like to execute from the
codesheet of the child. And, if I understand you
correctly, that's exactly what you've described.

Thanks,
Bill
 
And I now see that I too was wrong....I didn't realize that you could use
the Me.Parent.VariableName syntax!
 
And I now see that I too was wrong....I didn't realize that you could use
Hey, maybe we could get together and write code in the dark. <lol>

Reading that again - it doesn't sound like I meant it, but I'm sure *you* knew
what I meant. <groan>
 
I got it! < g >

--
Ken Snell
<MS ACCESS MVP>

Bruce M. Thompson said:
Reading that again - it doesn't sound like I meant it, but I'm sure *you* knew
what I meant. <groan>
 
Back
Top