Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Variables from UserForm to UserForm 2

Status
Not open for further replies.

kamfl610

Programmer
Apr 15, 2003
90
US
Got a question,

Can you pass variables from one userform to another? I need to pass a date parameter into another userform. How do I do that since when I userform.show the next userform the initialize procedure will wipe it out? Do I do it on the userform.load? Help would be much appreciated. Thanks!!
 
Perhaps the simplest way would be to use a global (public) variable defined in a code module. But you can reference a value in a text box on one form in another form, which does not require global variables:

Assuming there are two forms: UserForm1 and UserForm2 and UserForm1 has a text box and a command button, and UserForm2 also has a text box. (Note that both can be named TextBox1 because each only has meaning in the context of the form that contains it.)

UserForm1 code:[blue]
Code:
   Private Sub CommandButton1_Click()
     UserForm2.Show
   End Sub
[/color]

UserForm2 code:[blue]
Code:
   Private Sub UserForm_Activate()
     TextBox1.Text = UserForm1!TextBox1.Text
   End Sub
[/color]

1. Select UserForm1 and press F5.
2. Type something into the text box.
3. Click the command button.
4. Observe the same text appearing in the box on form2.

 
Thanks alot! It was very helpful & I gave you a star for it.
 
Hi kamfl610,

Userform variables only exist as long as the userform itself exists so you must, as you say, get hold of your variable from the first form before you unload it. However, showing a second form does not cause the first form to be deleted.

If your variable in the first form is exposed (i.e Public in scope) then you should be able to reference it from the second form by qualifying it with the form name. If you do unload your first form before you need the value you must arrange to save your variable somewhere else, perhaps in a public variable in one of your code modules.

Whereabouts in the second form you access the variable depends on when you want it.

Enjoy,
Tony
 
In the time it took me to write a reply the question was answered and the answer got a star. Time to give up! [smile]
 
Gave you a star anyway Tony just for replying and being knowledgable. Thanks!!
 
Goodness! I wasn't asking for that nor, probably, do I deserve it, but thanks very much all the same.

Tony [shadeshappy]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top