Hello.
I am delclaring a variable in the declarations area of the a module. So
when I show a form, I should be able assign a value to it, unload the form,
and still have the value of the variable... right? The variable is empty
after the user form is unloaded. Please help!

Signature
Thanks,
Mike
Doug Robbins - Word MVP - 06 Aug 2007 23:27 GMT
Make sure that you do not also have the variable declared in the
declarations section of the code in the form itself.

Signature
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
> Hello.
> I am delclaring a variable in the declarations area of the a module. So
> when I show a form, I should be able assign a value to it, unload the
> form,
> and still have the value of the variable... right? The variable is empty
> after the user form is unloaded. Please help!
Tony Strazzeri - 23 Sep 2007 02:02 GMT
Hi Mike,
> I am delclaring a variable in the declarations area of the a module. So
> when I show a form, I should be able assign a value to it, unload the form,
> and still have the value of the variable... right?
> --
> Thanks,
> Mike
Wrong!.
The variable's value won't be available after you unload the form.
You need to hide the form using the UserForm's Hide method. When you
are finished you can then unload it.
Cheers
TonyS.
fumei - 24 Sep 2007 19:29 GMT
If the Public variable is declared in a standard module, but set by a
procedure in a userform module, it persists if the userform is unloaded.
Standard Module:
Option Explicit
Public strWhatever As String
Sub ShowPubVar()
Msgbox strWhatever
End Sub
Userform Module (with just one commandbutton):
Sub CommandButton1_Click()
strWhatever = "Hello world."
Unload Me
End Sub
If you show the userform, click the commandbutton, it passes the string to
the Public variable, and unloads.
If you execute ShowPubVar afterwards, it displays the set value of the
variable. The userform does not have to Hide first.
Tony Strazzeri - 25 Sep 2007 02:37 GMT
> If the Public variable is declared in a standard module, but set by a
> procedure in a userform module, it persists if the userform is unloaded.
[quoted text clipped - 23 lines]
> --
> Message posted via OfficeKB.comhttp://www.officekb.com/Uwe/Forums.aspx/word-programming/200709/1
I stand corrected. I made the erroneous assumption that the Public
variable was declared in the Userform General declarations area not
the External module.
With a big grin and tongue firmly in cheek I say "my answer is correct
for the problem I define it to solve". Therefore of course my
comments do apply if the variable was declared in the Userform General
declarations area.
Cheers
TonyS.
fumei - 25 Sep 2007 18:31 GMT
Quite true.
It is all a matter of of scope, a much skipped over (IMO) area of
consideration. Simply put, a Public variable declared within a userform
module still only has scope within the life of the userform. When a userform
is unloaded all variables declared within it are gone. Keeping in mind, of
course, that Public arrays can not declared within an object (like a userform)
module.
So....declare it Public outside the userform module.