Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / September 2007

Tip: Looking for answers? Try searching our database.

Public Variable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike Archer - 06 Aug 2007 22:28 GMT
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.

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.