userforms

R

ranswrt

I have 2 userforms set up. How do I pass a value from one userform to the
other?
In userform1 I set a "public item as string" variable, but it didn't pass
that value to userform2 when it called userform2.

Thanks
 
J

Joel

One trick is to make an invisible text box on one userform and use it to
store variables that need to be passed.
 
R

ranswrt

In the userform1 module I have:

public item as string

Then on a button I have:

item = combobox1.value
userform2.show
 
P

paul.robinson

Hi
A public variable declared in a userform code module becomes an object
for that form, so that when you unload the userform, you lose the
value of the variable. You can hide Userform1 however,

In userform1 module you have this

Option Explicit
Public MyVar As String

Private Sub CommandButton1_Click()
MyVar = Me.TextBox1.Value
Me.Hide
UserForm2.Show
End Sub

And in Userform2 module you have this

Option Explicit

Private Sub CommandButton1_Click()
Unload Me
Unload UserForm1
End Sub

Private Sub UserForm_Initialize()
Me.TextBox1.Value = UserForm1.MyVar
End Sub

Since Userform1 is hidden when Userform2 is called, then Userform2 can
see MyVar as an object of Userform1.

You can also declare MyVar in a GENERAL code module. Now it is
genuinely Public. In Userform1 code module you now use

Private Sub CommandButton1_Click()
MyVar = Me.TextBox1.Value
Unload Me
End Sub

And for Userform2

Private Sub CommandButton1_Click()
Unload Me
End Sub

Private Sub UserForm_Initialize()
Me.TextBox1.Value = MyVar
End Sub

The first method is useful as it keeps the variable names with the
form and makes your code easier to read/maintain. I tend to only use
the first method.

regards
Paul
 
R

ranswrt

Thanks I'll give that a try

Hi
A public variable declared in a userform code module becomes an object
for that form, so that when you unload the userform, you lose the
value of the variable. You can hide Userform1 however,

In userform1 module you have this

Option Explicit
Public MyVar As String

Private Sub CommandButton1_Click()
MyVar = Me.TextBox1.Value
Me.Hide
UserForm2.Show
End Sub

And in Userform2 module you have this

Option Explicit

Private Sub CommandButton1_Click()
Unload Me
Unload UserForm1
End Sub

Private Sub UserForm_Initialize()
Me.TextBox1.Value = UserForm1.MyVar
End Sub

Since Userform1 is hidden when Userform2 is called, then Userform2 can
see MyVar as an object of Userform1.

You can also declare MyVar in a GENERAL code module. Now it is
genuinely Public. In Userform1 code module you now use

Private Sub CommandButton1_Click()
MyVar = Me.TextBox1.Value
Unload Me
End Sub

And for Userform2

Private Sub CommandButton1_Click()
Unload Me
End Sub

Private Sub UserForm_Initialize()
Me.TextBox1.Value = MyVar
End Sub

The first method is useful as it keeps the variable names with the
form and makes your code easier to read/maintain. I tend to only use
the first method.

regards
Paul
 
A

Alan

If you are calling UserForm2 open with UserForm1 you can use a
UserForm_Initialize event in UserForm2 to pass UserForm1.Combox1's value. You
don't really need to set a variable.

Sub UserForm_Initialize
UserForm2.Combox1.Value=UserForm1.Combox1.Value (sample)
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top