PDA

View Full Version : User Forms behaving strangely



wnazzaro
07-15-2008, 08:05 AM
I have four forms, one is the Master form that has three buttons to call the others. When a form is called, the Master is hidden and the other is shown. When one of the others is closed, the Master is opened again. Here is where the problem comes up, if I call the same form a second time, the code seems to freeze on the last line of code. Here's the code:
'Click button, form shows
Private Sub btnForm1_Click()
StartUp.Hide
form1.Show 'If I call the form a 2nd time, the code freezes here and I have to break in to stop it.
End Sub

'When form is closed, Master form is shown.
Private Sub UserForm_Terminate()
Unload form1
StartUp.Show
End Sub


I've tried changing Unload to Hide and vice versa, and it doesn't make a difference. Any thoughts?

Norie
07-15-2008, 11:12 AM
Why not unload the StartUp form rather than just hiding it?

mikerickson
07-15-2008, 12:55 PM
Unload is not performed until the end of a proceedure.
If that terminate event is in form1, StartUp.Show is performed before form1 is unloaded.

You might try (in StartUp)

Private Sub btnForm1_Click()
StartUp.Hide
form1.Show
form1.Unload
End Sub
(in form1)
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Cancel = true
Me.Hide
StartUp.Show
End Sub

wnazzaro
07-15-2008, 02:20 PM
You might try (in StartUp)

Private Sub btnForm1_Click()
StartUp.Hide
form1.Show
form1.Unload
End Sub
(in form1)
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Cancel = true
Me.Hide
StartUp.Show
End Sub

Still doesn't work. The code still gets hung up at form1.Show (but only the second time the form is called)

mikerickson
07-15-2008, 03:12 PM
How are you dismissing form1?
Also, are these UF's modal or modeless?
When trying the code I posted, did you remove the form1_Terminate event from the first post?
Is there a _Deactivate event for either form?

(If you attach a workbook with example userforms that demonstrate your problem, please be aware that my Mac can't handle either ActiveX sheet controls or modeless userforms.)

TomSchreiner
07-15-2008, 03:20 PM
It is better IMO to provide yourself with some explicit references and then take care of the cleanup yourself. It is the only way to know for sure that your form(s) is/are really gone.

For example. Three forms. frmMain, Form1, and Form2. frmMain contains CommandButton1,2.

Code in frmMain:

Option Explicit

Dim f1 As Form1, f2 As Form2

Private Sub CommandButton1_Click()
Set f1 = New Form1
Set f1.MainForm = Me
Me.Hide
f1.Show
End Sub

Private Sub CommandButton2_Click()
Set f2 = New Form2
Set f2.MainForm = Me
Me.Hide
f2.Show
End Sub


Code in both Form1 and Form2:

Option Explicit

Private pMainForm As frmMain

Friend Property Set MainForm(frm As frmMain)
Set pMainForm = frm
End Property

Private Sub UserForm_Terminate()
pMainForm.Show
Set pMainForm = Nothing
End Sub

RECrerar
07-17-2008, 01:28 AM
This seems to work for me. The following is i the main form and there are two buttons that call the two subforms

Private Sub CommandButton1_Click()
Me.Hide
SubForm1.Show
Me.Show
End Sub

Private Sub CommandButton2_Click()
Me.Hide
SubForm2.Show
Me.Show
End Sub

In the subforms, if you have a button that is used to exit them:

Private Sub CancelButton_Click()
Unload Me
End Sub

Will also run if you just click the little x in the corner of the form. You don't need a terminate event in the subforms as the code in the main form should just continue running when the sub form is closed.

Do let me know if I've missed something but this is the method I've been using when doing stuff likethis with userforms and so far have not had a problem with it

TomSchreiner
07-17-2008, 05:13 AM
My method works rather the forms are modal or modeless...