PDA

View Full Version : [SOLVED] Muliple Userforms



Kindly_Kaela
01-05-2007, 07:44 AM
I have a feeling I'm using forms wrong, and looking for a little VBA-101 lesson.

The program I'm creating requires several different windows of questions for the user to answer. The user hits NEXT, and a new screen opens. Depending on the users answers, different windows may open.

Currently, I'm creating a new UserForm for each new screen. Ok, now stop laughing and help this gal out. :tongue:

The basic commands I've been using are:



UserForm2.Show
Unload Userform1
UserForm1.Hide (I haven't used this one)


Can anyone please give me some basic lessons on how I should be designing forms? ...along with the commands that go with changing from screen to screen after the user hits NEXT.

If there's anything I'm leaving out, please ask and I'll respond quickly.

Thanks!
Kaela
:cloud9:

P.S. I hope it's ok that I posted this in the WORD section too, not sure if the same geniouses go to both sections.

XLGibbs
01-05-2007, 07:54 AM
You would likely want ALL of the userforms loaded at the initial first forms Form_Load.

The can be loaded and not visible...

A form once loaded into memory can be unloaded using

UserForm1.Unload

But any code associated with that form will close as well. it would be best to use only

UserForm1.Hide

as this keeps the form in active memory.

As far as the code to move between forms, you would have abutton saying "next"

That button code would have your logic as to which form to show next.
(assuming you have their answer as some variable in the code)


Private commandButton_Click....etc
If ans = 1 then
UserForm2.Show
Else
UserForm3.Show
End if
End sub


Me.Hide

lucas
01-05-2007, 07:55 AM
Basically unload takes it out of memory. hide...it will retain input..
consider a multipage userform. from the userform toolbox rightclick on the toolbox and select "additional controls"

XLGibbs
01-05-2007, 08:02 AM
You can also use one form, or a multi page forms and make various parts visible = False (or .enabled = false) until they are needed or certain answers are provided

lucas
01-05-2007, 08:06 AM
Cross posted in this forum:
http://vbaexpress.com/forum/showthread.php?p=84771#post84771

kk please read this (http://www.excelguru.ca/node/7)
If you wish to ask the same question in multiple forums please provide a link.

Kindly_Kaela
01-05-2007, 08:13 AM
Wow Gibbs, a response I actually understand fully. YAY :thumb

By keeping the forms in memory, will the program be using more CPU / RAM and essentially slow down my computer?


You would likely want ALL of the userforms loaded at the initial first forms Form_Load.

....UserForm1.Hide, as this keeps the form in active memory.

Kindly_Kaela
01-05-2007, 08:16 AM
Hey Lucas! I've never played with multipage userforms. I found the "additional controls" you mentioned. How do these options help me? There's so many and I can't seem to find a connection.


consider a multipage userform. from the userform toolbox rightclick on the toolbox and select "additional controls"

Kindly_Kaela
01-05-2007, 08:20 AM
Can I get rid of the tabs at the top where PAGE 1, PAGE 2, ect. are? I don't want the user to be able to use the tabs, instead they just use my NEXT button.

Keeping in mind how inexperienced this girl is....can you write out the full commands that go from page to page?

Thanks Gibbs!



You can also use one form, or a multi page forms and make various parts visible = False (or .enabled = false) until they are needed or certain answers are provided

lucas
01-05-2007, 08:27 AM
Can I get rid of the tabs at the top where PAGE 1, PAGE 2, ect. are? I don't want the user to be able to use the tabs, instead they just use my NEXT button.

Keeping in mind how inexperienced this girl is....can you write out the full commands that go from page to page?

Thanks Gibbs!
Don't think you can get rid of the tabs but as Pete has suggested you can restrict them moving to the next tab until data is filled in. Attached is a simple example with next and back buttons:

Kindly_Kaela
01-05-2007, 10:03 AM
Is there a way to update a hidden form without showing it?

Let's say for example you want to make TextBox1.Visible = True on a hidden UserForm. But the form does not SHOW just yet.

BTW, thanks for the example Lucas, very helpful. :mkay

Bob Phillips
01-05-2007, 10:14 AM
Wow Gibbs, a response I actually understand fully. YAY :thumb

By keeping the forms in memory, will the program be using more CPU / RAM and essentially slow down my computer?

Don't go this route, worst possible IMO. Say you have a possible 70 forms but only 10 questions get asked depending upon the answers, loading 70 to use 10 is a waste of valuable resources.

I would do it like this, assuming that there is a back action (they can go back)

Show Userform1

Depending upon answer, show another form, like so



Private Sub cmdNext_Click()
Me.Hide
If OptionButton1.Value Then
UserForm2.CalledBy = Me.Caption
UserForm2.Show
Else
UserForm3.CalledBy = Me.Caption
UserForm3.Show
End If
End Sub


I have code this into option button values, but it could just as easily be a Yes/No answer. I would also tend to build a module to determine the routing order, so as to keep the forms uncluttered.

Note how the parent's caption is passed to the child, this is to facilitate going back. The routing table mentioned above could also be used to do this.

Then in the child forms, you would have similar code to theparent to route on, and code like this to go back



Public CalledBy As String

Private Sub cmdBack_Click()
Dim oUserform As Object
Me.Hide
Set oUserform = UserForms.Add(CalledBy)
oUserform.Show
End Sub

Bob Phillips
01-05-2007, 11:15 AM
Don't think you can get rid of the tabs but as Pete has suggested you can restrict them moving to the next tab until data is filled in. Attached is a simple example with next and back buttons:

lucas
01-05-2007, 11:20 AM
You Rascal.....thanks Bob...new to me.

lucas
01-05-2007, 11:21 AM
.Pages(1).Visible = False
that's good to know