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 / Excel / Programming / August 2008

Tip: Looking for answers? Try searching our database.

User form varible passing

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
miek - 18 Aug 2008 15:16 GMT
In my VBA module "module1" i have a global varible

   Private global_var1 as boolean
I initialize it in the main Subroutine
  global_var1 = false
Then I call my userform, which has a cmdbtn "Doit"
  With Question_Form
    Load Question_Form
    .Show
   End With
My userform cmdbtn code is
  Private Sub Always_Btn_Click()
   global_var1 = True
   Question_Form.Hide
  End Sub

However, when my userform returns "global_var1" remains = false

How do i pass global_var1 back to module1?
Thanks
Rick Rothstein (MVP - VB) - 18 Aug 2008 15:28 GMT
Does the "main Subroutine" get run every time that you show your UserForm?
If so, you are probably re-initializing the global variable every time.
Since a Boolean variable automatically initializes on creation as False,
there is no need to deliberately initialize it to this value... try removing
the global_var1=False statement from your "main Subroutine" and see it that
makes a difference.

Rick

> In my VBA module "module1" i have a global varible
>
[quoted text clipped - 16 lines]
> How do i pass global_var1 back to module1?
> Thanks
Rick Rothstein (MVP - VB) - 18 Aug 2008 16:02 GMT
'lcaretto' spotted the real problem... you Dim'med the global variable as
Private (I completely overlooked this), thus restricting its scope to code
in its Module only.

However, the point I made about the Boolean variable being initialized to
False at creation (so you don't have to initialize it manually) is still
valid.

Rick

> Does the "main Subroutine" get run every time that you show your UserForm?
> If so, you are probably re-initializing the global variable every time.
[quoted text clipped - 25 lines]
>> How do i pass global_var1 back to module1?
>> Thanks
miek - 18 Aug 2008 16:58 GMT
The Subroutine only runs once
I have now set my global_var1 as follows:
When ' global_var1 is comes back from "Question_form" ' global_var1 is = False

  Dim global_var1 as boolean

  Sub module1
    ' global_var1 = false  ' global_var1 is = False, auto initilized
    With Question_Form
      Load Question_Form
      .Show
    End With
    ans = global_var1 ' global_var1 is = False
  End Sub ' module1

My userform cmdbtn code is

  Private Sub Doit_Btn_Click()
    global_var1 = True  'global_var1 is = False
    Question_Form.Hide 'global_var1 is = True
  End Sub

> 'lcaretto' spotted the real problem... you Dim'med the global variable as
> Private (I completely overlooked this), thus restricting its scope to code
[quoted text clipped - 35 lines]
> >> How do i pass global_var1 back to module1?
> >> Thanks
lcaretto - 18 Aug 2008 15:49 GMT
Try making global_var1 a public variable so that it will be visible outside
of module1.

> In my VBA module "module1" i have a global varible
>
[quoted text clipped - 16 lines]
> How do i pass global_var1 back to module1?
> Thanks
 
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.