Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
userforms
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 |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
userforms
One trick is to make an invisible text box on one userform and use it to
store variables that need to be passed. "ranswrt" wrote: 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 |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
userforms
In the userform1 module I have:
public item as string Then on a button I have: item = combobox1.value userform2.show On userform2 I try to use the item variable, but it's not passed to userform2. What am I doing wrong? "Joel" wrote: One trick is to make an invisible text box on one userform and use it to store variables that need to be passed. "ranswrt" wrote: 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 |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
userforms
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 On Apr 17, 2:36*pm, ranswrt wrote: 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 |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
userforms
Thanks I'll give that a try
" wrote: 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 On Apr 17, 2:36 pm, ranswrt wrote: 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 |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
userforms
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 "ranswrt" wrote: In the userform1 module I have: public item as string Then on a button I have: item = combobox1.value userform2.show On userform2 I try to use the item variable, but it's not passed to userform2. What am I doing wrong? "Joel" wrote: One trick is to make an invisible text box on one userform and use it to store variables that need to be passed. "ranswrt" wrote: 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 |