Passing a Variable Question

G

Guest

On my Main form, I have a Cancel button that closes a subform. If a
particular button (AddID) on the subform has been used (to complete a
particular field), the Cancel button performs additional operations. (The
field can be completed manually, but Cancel doesn't need to perform the
additional operations in that case.) I have code on the subform's
AddID_Click that sets a Boolean variable to tell Cancel that this has
occurred, but the variable seems to have disappeared if I click Cancel. I
have tried Dim and Public delcarations in a standard module and in both form
modules to no avail. The variable does get properly set on AddID_Click, but
the Main form sees the variable as Empty.

I would appreciate come guidance.

Thanks in advance.
 
A

Albert D.Kallal

On my Main form, I have a Cancel button that closes a subform.

Not to split hairs, but a subform does have a particular meaning in
ms-access, and as normal rule, you can NOT close a sub-form.

Since you are talking about closing this form, I will simply assume you have
two separate forms open (if not, then you need to correct this assuming of
mine..and ignore the rest of this post!!). You can't close a sub-form..since
it is inside of existing form already...your only choice here is to close
the main form which contains the sub-form.
I have code on the subform's
AddID_Click that sets a Boolean variable to tell Cancel that this has
occurred, but the variable seems to have disappeared if I click Cancel.

So, both the Cancel, and AddID_click on are this form..correct?
(as always, one forgets the most important detail...is the cancel button
on the same form?).
have tried Dim and Public delcarations in a standard module and in both
form
modules to no avail.
The variable does get properly set on AddID_Click, but
the Main form sees the variable as Empty.

Ouch..reading this far...now me thinks that you do actually have a sub-form,
but how could you ever closed this sub-form???
(ok...lets make some new assumptions here..and again you can correct me if I
am wrong. So, we actually do have a sub-form,
but I then have to explain that you can't close a sub-form...only the main
form (hence my confusing with your above notes about
wanting to close the sub-form).

So, if we declare the Boolean var in the sub-forms module (I think this is a
good idea, and also a good place
to put the var since we might re-use the sub-form later on).

public bolMyVar as boolean

The reason why we are declaring the var public, is that we want other
routines to be able to use this value. However, what happens if we have 10
forms open, and each of those 10 forms have a sub-form also...which variable
are your referencing? So, the simple answer here is that you need quality
the sub-forms reference here.

So, in you man form, you can reference the value of the sub form as
following

me.MySubFormContorl.Form.bolMVar

Note how the above syntax has MySubFormContorl. A sub-form control can have
a different name then that of the actual form that the sub-form control
points to. (this is important to understand this....a sub-form is actually a
control with a name...and you THEN set what sub-form this control will
display. They often have the same name..but, they don't have to...).

So, the final answer here is that you simply quality the forms reference....

if me.MySubFormContorlName.Form.bolMyVar = true then
.......
end if
 
G

Guest

Although you can't really close a subform, you can accomplish the same thing
with
Me!MySubFormControl.SourceObject = ""

As to the problem with the variable, this can be accomplished with a Static
Function which could be in a standard module, but may work in the main form's
module (but I haven't tested it that way.)

Static Function DoOtherStuff(Optional varOkGo as Variant) As Boolean
dim blnRetVal as Boolean

If Not IsMissing(varOkGo) Then
blnRetVal = varOkGo
End If
DoOtherStuff = blnRetVal

End Function

You set the value of the function by passing it a value:
DoOtherStuff(True)

To determine the current value of the function:

If DoOtherStuff() = True Then
'Do True Things
Else
'Do False Things
End If
 
A

Albert D.Kallal

Klatuu said:
Although you can't really close a subform, you can accomplish the same
thing
with
Me!MySubFormControl.SourceObject = ""

Yes, but it is unlikely that the original poster is doing that. Since the OP
mentioned a form close, it does confuse the issue here.

I suppose it helps to point out that on can in a round about way close a
sub-form. However, I would
be more interested in how the OP is actually closing the form.

Often, those that are new to ms-access call a related form a sub-form, when
in fact they mean a separate form. This issue is critical in a correct
answer to this person (since the way the form's reference will have to be
changed).
As to the problem with the variable, this can be accomplished with a
Static
Function which could be in a standard module, but may work in the main
form's
module (but I haven't tested it that way.)

Well, you certainly do NOT WANT to use a global function here, as that is
just as bad as using a global variable, which should be avoided at all cost.

The problems of doing this are

if you copy the form to another application, you have to remember to
find, search and grab that global function in the standard module

If you have multiple copies of the form running, or the sub-form is used
in more then one place, they will step all over each other

Often, during development, one will make a copy of existing form as a
starting point. Once again, even making a copy of this form that uses a
global variable/function means you open up more bugs, or again have to
remember to find the dependencies of this form and change the name (or,
remember to not have the two forms open at the same time).

By moving the variable stetting to a global function, you make the
application harder to understand, and further harder to debug, as now code
is outside of the forms module, and there is not reason for this.


So, at the very least, you would want that code in the forms module.
However, why go to all that trouble when you can just define a simple public
variable in the form, and use a correct reference to that variable?

At the end of the day, yours is just a another suggestion and effort to
help!! So, in that light..this is all good, and we can't argue with that!!

However, we do want to teach HARD against using global vars (or static
functions) when there is no need at all...
 
G

Guest

Yes, I understood your original post. It is difficult sometimes when
incorrect terminology is used. I think the most frequent error is refering
to a form control as a field.

In any case, I find static functions useful for this sort of problem
regardless of whether it is truely a subform or a related form.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top