Confusion about context of variable

D

davegb

I'm working on some code that asks the user via userform to select a
checkbox if they want a certain result. I've publicly declared a
boolean variable bHdr to capture whether they've selected the box
(true) or not (false). I checked to made sure the result of that
selection was passed from the userform code back to the original macro
by putting a watch on the variable in the userform code. When control
passed back to the original macro, the value of bHdr became "empty".
Then I put a separate watch on the variable bHdr in the original code.
It shows the variable having a value "true" if I checked the
checkbox.

I'm confused about this. If the variable is declared publicly, why is
it empty in the context of the userform once it is closed, but true in
the context of the module? What does this mean? Exactly how does VBA
handle this kind of variable?
 
P

paul.robinson

Hi
Public variables defined in code modules behind userforms are
properties of the form, so I guess they are destroyed when the form
is.
On the other hand, if your public variable IN THE CODE MODULE FOR THE
USERFORM is called TestBoolea.n thenwhen you call the userform you can
do:

Userform1.Show
myBoolean = Userform1.TestBoolean
Unload Userform1

regards
Paul
 

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