PDA

View Full Version : Initializing a userform from another userform?



Sock
07-22-2013, 10:52 PM
Hi everyone,

I'm trying to initialize another user form from within a different one for convenience's sake.

The first is "UserForm1" the second is "UserForm2". I'm trying to initialize UserForm2 from UserForm1. In UserForm1, I can use:


UserForm2.show

However, when I use this, it seems to skip the initialization code. Every time UserForm2 is closed, it is closed with:


Unload Me
End

So I feel like it shouldn't be loaded in memory.

I have a set of textboxes in UserForm2 that I want to be pre-filled when I load up UserForm2 from UserForm1, as well as change some captions when UserForm2 is loaded from UserForm1. Unfortunately, I get the default "UserForm2" every time I try to load it from UserForm1.

Does anyone have any ideas for what my problem is? Maybe I'm going about it the wrong way.

Thanks so much in advance. Your help is greatly appreciated!

snb
07-23-2013, 04:39 AM
Why using 2 userforms in the first place (it's seldom, if ever, necessary) ?

mikerickson
07-23-2013, 06:50 AM
This worked for me

' in userform1's code module

Private Sub CommandButton1_Click()
With UserForm2
.TextBox1.Text = "some text"
.TextBox2.Text = Me.TextBox1.Text
.Show
End With
End Sub

SamT
07-23-2013, 07:05 AM
Is the name of UserForm2's initializing sub
Private Sub UserForm_Initialize()

Sock
07-23-2013, 08:31 AM
Why using 2 userforms in the first place (it's seldom, if ever, necessary) ?

Hi snb. Good question! It's there as an 'edit' button, actually. The edit userform is basically the same thing as the 'add' userform, so it would be easy to keep the two together (bar a little bit of extra coding which I've already done). The only thing that would change is just how I'm captioning a couple things and some pre-included text in textboxes just for the user's sake.

I guess in a way it's not 'necessary' but it is 'nice', if that makes sense :P LOL!


This worked for me

' in userform1's code module

Private Sub CommandButton1_Click()
With UserForm2
.TextBox1.Text = "some text"
.TextBox2.Text = Me.TextBox1.Text
.Show
End With
End Sub

Hi Mike! Thanks! I'll give this one a shot when I get the chance. :)


Is the name of UserForm2's initializing sub
Private Sub UserForm_Initialize()

Hey Sam. I now realize it's actually
Private Sub UserForm2_Initialize()

Does this make a big difference? If so, I'd be curious as to why that might change it. This seems like a good learning opportunity.

Thank you to you three for your responses! Without you guys I'd still be trying to figure out how to add two and two in VBA :rotlaugh:

SamT
07-23-2013, 06:45 PM
Hey Sam. I now realize it's actually
Private Sub UserForm2_Initialize()

Does this make a big difference? If so, I'd be curious as to why that might change it. This seems like a good learning opportunity.

Yes it does.

In VBA you can get VBA to correctly insert the desired sub declaration by selecting the object (Userform, Workbook, TextBox, etc) in the left hand dropdown above the code page and the selecting the desired event from the right hand dropdown.

As to why it makes a difference: Think of the Initialize event sub as the event handler, not the event detector. The detector is actually the OS, not VBA or the Form. The Windows or MAC OS has no way of knowing what name you're giving to things so it can only pass Events using generic names.

All Event "handling" subs for all VBA Objects are named using the pattern:

Class Type_Name + Underscore + Event Name

Note that UserForm Objects, ie Controls, use the Control's name, but then Controls are not thought of as Class Objects.

Thank Bill Gates that Microsoft did all that background coding for us, 'cuz its complicated.

snb
07-24-2013, 01:15 AM
Dive into the userform resources like 'multipage' or hiding/showing a frame, containing extra controls.

Zack Barresse
07-24-2013, 12:26 PM
Generally the only time I'm calling one userform from another is when I'm using a progress bar. The way I do it is to declare a variable of that object. So if your second userform is titled UserForm2 (I never leave them as generic), it would look something like this...

Dim ufSecondForm As New UserForm2
ufSecondForm.Show
You can name the variable whatever you want so long as its unique.

Sock
07-27-2013, 11:14 AM
Sorry for the late response. I've been swamped.


Yes it does.

In VBA you can get VBA to correctly insert the desired sub declaration by selecting the object (Userform, Workbook, TextBox, etc) in the left hand dropdown above the code page and the selecting the desired event from the right hand dropdown.

As to why it makes a difference: Think of the Initialize event sub as the event handler, not the event detector. The detector is actually the OS, not VBA or the Form. The Windows or MAC OS has no way of knowing what name you're giving to things so it can only pass Events using generic names.

All Event "handling" subs for all VBA Objects are named using the pattern:

Class Type_Name + Underscore + Event Name

Note that UserForm Objects, ie Controls, use the Control's name, but then Controls are not thought of as Class Objects.

Thank Bill Gates that Microsoft did all that background coding for us, 'cuz its complicated.

Thank you for the quick lesson! This is really interesting.


Generally the only time I'm calling one userform from another is when I'm using a progress bar. The way I do it is to declare a variable of that object. So if your second userform is titled UserForm2 (I never leave them as generic), it would look something like this...

Dim ufSecondForm As New UserForm2
ufSecondForm.Show
You can name the variable whatever you want so long as its unique.

That seems to be an efficient and clean method. I'll try to do that in the future as well. Thanks! :)


Dive into the userform resources like 'multipage' or hiding/showing a frame, containing extra controls.

Will do, sir! Thanks for the help!