WUGNET, the Windows User Group Network
Your Complete Resource Center for "The Best" in Shareware, Computing Tips and Support, Windows Industry News... and much more!
Home Forums Shareware Windows Tips Hot Offers FREE Newsletters Arcade Contact Us About Partners
Search WUGNET: RSS Feeds RSS Feeds Advertise with WUGNET    |    Shareware eBooks
HomeHome FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Public Variable

 
   Home -> Office -> User Forms RSS
Next:  Public Variable  
Author Message
Mike Archer

External


Since: Jul 17, 2006
Posts: 7



(Msg. 1) Posted: Mon Aug 06, 2007 2:28 pm
Post subject: Public Variable
Archived from groups: microsoft>public>word>vba>userforms (more info?)

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!
--
Thanks,
Mike
Back to top
Login to vote
Doug Robbins - Word MVP

External


Since: Jul 14, 2006
Posts: 4208



(Msg. 2) Posted: Tue Aug 07, 2007 8:27 am
Post subject: Re: Public Variable [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Make sure that you do not also have the variable declared in the
declarations section of the code in the form itself.

--
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

"Mike Archer" <MikeArcher.TakeThisOut@discussions.microsoft.com> wrote in message
news:C40CE203-7926-48B4-BFC8-533F0CB1E707@microsoft.com...
> 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!
> --
> Thanks,
> Mike
Back to top
Login to vote
Tony Strazzeri

External


Since: Jun 27, 2007
Posts: 3



(Msg. 3) Posted: Sat Sep 22, 2007 6:02 pm
Post subject: Re: Public Variable [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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.
Back to top
Login to vote
fumei via OfficeKB.com

External


Since: Sep 21, 2007
Posts: 24



(Msg. 4) Posted: Mon Sep 24, 2007 6:29 pm
Post subject: Re: Public Variable [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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.

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200709/1
Back to top
Login to vote
Tony Strazzeri

External


Since: Jun 27, 2007
Posts: 3



(Msg. 5) Posted: Mon Sep 24, 2007 6:37 pm
Post subject: Re: Public Variable [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Sep 25, 4:29 am, "fumei via OfficeKB.com" <u37563@uwe> wrote:
> 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.
>
> --
> 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.
Back to top
Login to vote
fumei via OfficeKB.com

External


Since: Sep 21, 2007
Posts: 24



(Msg. 6) Posted: Tue Sep 25, 2007 5:32 pm
Post subject: Re: Public Variable [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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.

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200709/1
Back to top
Login to vote
Display posts from previous:   
       Home -> Office -> User Forms All times are: Eastern Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Categories:
 Windows XP
 Windows Vista
 Windows Other
  Office
 Office Other
 Security
 WinRAR
  • Home |
  • Shareware |
  • Windows Tips |
  • Hot Offers |
  • FREE Newsletters |
  • Arcade |
  • Forums |
  • eBooks |
  • About WUGNET |
  • Partners |
  • Contact

  • WUGNET Privacy Policy |
  • Link to WUGNET