Global variables

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

Guest

I have defined global variable in main form, I want
to use it in subform. Do I have to open the subform
using these global variables? If so, then what is the syntax? I know how to
pass just one variable, but not
multiple variables.

I was under the impression that I will be able to use global
variables in subform without passing them.

What's best way to do this?

Also, my subform isn't linked with any table.

I am trying to accept a few fields that are linked to
the main form using subform, as there should be only
one set of entries for these fields.

Is this the right approach?

Thank you in advance,
-Me
 
How did you declare the global variable in the main form? If it's Public,
then your subform can see it. If it's Private, then the subform cannot see
it.
 
You cannot declare global variables in a form...their scope would be limited
to the module (in this case the form). Global variables (if they should be
used at all) must be declared in a module.

There may be a way to do what you want without using a variable at all, but
your objective isn't very clear making it difficult to suggest things. For
example, if you're just trying to get the value for a field in the parent
form and use it in your subform, you could use Me.Parent.SomeFieldName. Or
you could create a property or function in the parent form that returns the
values that you want. In any case, you should minimize the scope of any
variables you declare....to do otherwise is a bad programming practice.
 
Paul,

I agree with you. I think your suggestion should work for me.
I am trying to use Me.Parent.REQUESTER in my subform, where REQUESTER is the
field name in parent form and table associated with it. But to startwith,
when I try to display using Me.Parent.REQUESTER, I get the error

runtime error 2452

"The expression you entered has invalid reference to the parent property"

what am I doing wrong?

Thank you very much for your time!
-Me
 
You'll see that error if you open the subform by itself...but if it is
actually a subform on the parent form, you won't. Sounds like you need to
add an error handler in any case.
 
Paul,

No, I am calling this subform from main form.
Here is how I have created this subform, maybe that itself
is the wrong way. Can you let me know?

I created a subform just the way we create a form and
I am calling it from the main form. Is this the right
way to do it? I haven't defined any record source for this
subform.

But I did try defining the record source the same
as the main form, still it doesn't work.

Here is what I intend doing, I have a main form
and I would like to accept 4 fields using the subform.
Subform will be called by pressing a button. Behind this
button I will call DoCmd.OpenForm <formname> ...

Is this the correct way to do or you have other ideas?

Thank you very much again for your time!

-Me
 
OK...you aren't describing a subform then....it is just another form. A
subform is contained within and part of a parent form.

You can refer to the controls or fields of the main form from the dialog
form, i.e.,

Forms("SomeFormName").SomeFieldName

or

Forms![SomeFormName]![SomeFieldName]
 
Back
Top