PDA

View Full Version : Using two Userforms.



Emoncada
08-18-2008, 06:35 AM
I have 2 userforms that work with each other.
a cmdButton calls the other userform. What i want is for when the second form is closed to show userform1, but also be able to click the cmdbutton again to go back to userform2 if necessary and be able to close that one and go back to userform1.
Hope that's not that confusing.

I got this script that works nice from an earlier thread.

UserForm2
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then

UserForm1.Show
End If
End Sub
Problem is if I click on the CmdButton on UserForm1 to call This userform2 it gives me
RUN-TIME ERROR '400'
FORM ALREADY DISPLAYED; CANT SHOW MODALLY

Can someone help.

Thanks

Kenneth Hobs
08-18-2008, 08:14 AM
A review of what I said in that thread, http://vbaexpress.com/forum/showthread.php?t=21569 might be worthwhile. I had anticipated this issue there.

Notice, that I gave two methods to verify that a userform was active. You can use the Visible property or the API method using FindWindow.

e.g.

If UserForm1.Visible=True then AppActivate UserForm1.Caption
As discussed in that thread, you can use the UserForm1.Show method as well. However, it does reposition the dialog. Using Show will force the dialog to open if it is not open so that might be good or bad. The choice is yours.

What is your ShowModal value for the userform? You can set it to True as I explained in that other thread if you want it modeless. Or, you can set it in the show like:

UserForm1.Show vbModeless
Using these concepts, you should be able to avoid the error.

Bob Phillips
08-18-2008, 11:13 AM
Tuck the form away as you exit



Private Sub cmdShowForm2_Click()
Me.Hide
UserForm2.Show
End Sub



and do the same with form2



Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then

Me.Hide
UserForm1.Show
End If
End Sub

Emoncada
08-18-2008, 11:38 AM
I tried that xld but if I go back to userform2 the second time and click the red X it doesn't do anything.

Emoncada
08-19-2008, 05:55 AM
Any Idea how to get around that?