Hiding a userform properly!

R

Robert Crandal

Please take a look at the code below which my workbook
is using:
'--------------------------------------------------------
Private Sub Workbook_Open()
Userform1.Show
End Sub
'-------------------------------------------------------
Private Sub UserForm_Initialize()
MsgBox "Initialize stuff here"
Userform1.Hide
MsgBox "Okay, form1 is hidden now!"
Userform1.Show
End Sub
'--------------------------------------------------------

So....obviously this code will not work. I'm guessing that
the second call to "Userform1.Show" is causing the program
to crash. I would like to temporarily hide the form and then
make it visible again. What am I doing wrong?

thanks!
 
P

Peter T

Not clear what the real objective is, it'd be better if you explain.

If you merely want to run the form's initialize event without showing it,

Load Userform1 '
' or anything that references the form
' initialize fires

when you want to show it (and run it's Activate event)
Userform1.show

In passing, as looks like you are trying to run the form in the workbook
open event, probably better to run it with an onTime macro, but it depends.

Regards,
Peter T
 
R

Robert Crandal

Peter,

Okay, I'll try to explain my situation more....

I actually have 2 userforms. Userform1 is typically
displayed first because it contains buttons and other
controls. There is a button on top of Userform1 which
is designed to show Userform2.

If Userform2 is displayed, I would like to hide
Userform1. Once Userform2 gets closed out, I would
like to re-display the same Userform1 again or bring
it out of its previous hidden state.

I realize that I can leave both Userform1 and Userform2
visible at the same time, but I prefer to keep Userform1
out of view if Userform2 gets triggered. Just wondering
if this is possible.

thanks

Robert
 
P

Paul Robinson

Hi
The intialize code is event code - it fires when the form is loaded or
shown. The crash is probably due to the .show being inside the
initialize event code, which then fires the initialize, which does
the .show, which fires...
Don't put .Show or .Hide inside the initialize event. The initialize
simply fills your form with information that you want to see
when .Show happens.
Sounds like all you need is to put
Me.Hide
Userform2.Show

inside your event code for OKButton_Click in Userform1 (or whatever it
is called) and similarly
Me.Hide
Userform1.Show

inside your event code for OKButton_Click in Userform2
regards
Paul
 
P

Peter T

Paul has already covered most of what I would have said.

Whilst I follow what you describe below, I don't follow it in relation to
what you originally posted at all, no way to slot in stuff about hiding the
form and showing some other within the Initialize event. Is this two
different topics perhaps.

Regards,
Peter T
 

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