#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 191
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 191
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 789
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 191
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 152
Default 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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Userforms Eddie[_8_] Excel Programming 5 August 26th 05 08:19 PM
Userforms Ernst Guckel[_4_] Excel Programming 3 March 24th 05 12:13 AM
userforms [email protected] Excel Programming 0 February 16th 05 06:48 PM
Userforms nath Excel Programming 1 May 20th 04 04:53 PM
Userforms Paul Clark Excel Programming 2 January 17th 04 04:51 PM


All times are GMT +1. The time now is 01:52 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"