View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson[_4_] Jim Thomlinson[_4_] is offline
external usenet poster
 
Posts: 1,119
Default UNDERSTANDING VARIABLE SCOPE

If the form is not unloaded then you do not need to store the value. You can
just have the one form reference the values of the other form. Something like

userform2.textbox1.text = userform1.textbox1.text

This works so long as user form 1 is loaded. If not then it will throw an
error. If you want to use global variables to store values they should be
created (most likely) in modules. This is because modules run for the
duration of the program. Forms will go in and out of existence and globals
will be created and destroyed with the form. That being said if the global
only makes sense if the form is in existence then put it in the form. It will
only be created with the form and end with the form and this is a more
efficent use of memory.
--
HTH...

Jim Thomlinson


"-JEFF-" wrote:

Will try that. I unload it because I thought I had to in order to open up
other forms dependingon what buttons are clicked. How do I create a public
variable that is visible to all of the userform control subs? I have been
coding all of my VBA in the userform control subs, would it be better
practice (or easier to control) to have the click() subs call procedures in a
module and then return?
-JEFF-

"Jim Thomlinson" wrote:

The values in the form are being discarded because you are unloading the
form. Instead of unloading the form just hide it. By doing this the values
will not be lost and you will not need to store them anywhere. The next time
you show the forms it look just like th user left it before it was hidden.
--
HTH...

Jim Thomlinson


"-JEFF-" wrote:

I am trying to maintain variables value so when I return to a userform that
has already been used, it will already be filled out (as long as the user has
previously visited it. I thought declaring the variables as public at the
module level would make them visible in all subs but it is not working.

In module 3 I have variables declared as public. In the sub, the variables
are not declared because I want to use to publics but they seem to get over
written when they are assigned a value. ie.

MODULE 3
public v1 as string
public v2 as string

private sub MyButton_click()
v1 = "myValue"
v2 = "myValue"
end sub

The watch window shows <Out of context after values have been assigned
inside the sub.