Muliple Userforms and multiple Stacks

P

Peter T

From code initiated in my main form1, I want to hide form1 and show form2.
When done with form2, then re-show form1.

No matter which way I try and do this, my code runs up in multiple stacks
which don't terminate until final exit. In the example below, if I switch
forms several tmes by pressing CommandButtons 1 & 2 on main form1, only when
I finally exit are all the procedures completed.

This contrived example this does not pose a problem. But with a lot of
complex code involved I get a memory leak in some circumstances - in
particular, when I switch forms from a popup commandbar that was created
while on the main form.

This memory leak persists after I exit my app. However it is a one off, ie
having switched forms once in any given session of Excel, no further leak
occurs.

Switching forms from an OnTime macro does not appear to help.

Clearly there I'm doing something wrong and would appreciate pointers.

TIA,
Peter T

'//Userform1 with 3 CommandButtons

Private Sub CommandButton1_Click()
Debug.Print "UF1 UserForm2 start"
Me.Hide
UserForm2.Show

Me.Show
Debug.Print "UF1 UserForm2 end"
End Sub

Private Sub CommandButton2_Click()
Debug.Print "UF1 run Proc1 start"
Proc1
Debug.Print "UF1 run Proc1 done"
End Sub

Private Sub CommandButton3_Click()
Debug.Print "Unload UF1"
Unload Me
End Sub

'// end Userform1


'//Userform2 with 1 CommandButton

Private Sub CommandButton1_Click()
Debug.Print "unload UF2 "
Unload Me
End Sub
'// end Userform2



'// in a normal module
Sub test()
UserForm1.Show
End Sub

Sub Proc1()
Debug.Print "Proc 1 start"
Proc2
Debug.Print "Proc 1 done"
End Sub

Sub Proc2()
Debug.Print "Proc 2 start"
Proc3
Debug.Print "Proc 2 done"
End Sub

Sub Proc3()
Debug.Print "Proc 3 start"
UserForm1.Hide
UserForm2.Show
'comes back here when UF2 unloads
UserForm1.Show
Debug.Print "Proc 3 done"
End Sub
'// end normal module
 
K

keepITcool

sub ShowForm1
userform1.show
end sub

Option Explicit
'code for userform1
Private Sub CommandButton1_Click()
Me.Hide
UserForm2.Show
End Sub

Private Sub UserForm_Activate()
MsgBox "UF1 show"
End Sub
Private Sub UserForm_Initialize()
MsgBox "UF1 load"
End Sub
Private Sub UserForm_Terminate()
msgbox "UF1 done"
End Sub


Option Explicit
'code for userform2
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub UserForm_Terminate()
UserForm1.Show
End Sub


--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Peter T wrote :
 
P

Peter T

Hi KeepItCool - thanks for responding

I had tried similar to your suggestion before. It works for a 2 way switch
between forms 1 & 2, but then seizes on form 2 the second time it is
show'ed.

UF1 > CB1 > Me.Hide > UF2 show 'OK
UF2 > CB1 > Me.Hide > UF2 Term' > UF1 show 'OK
UF1 > CB1 > Me.Hide > UF2 show 'OK

now UF2 is displayed but no way to exit. No event is triggered with
CommandButton1_click and cannot close UF2 with the little "x",
need to reset the project!

Thoughts ?

Regards,
Peter T
 
K

keepITcool

ahh!...

are your forms Modeless or Modal?

if your forms showmodal properties are false
(and thus are shown modeless by default)
then my code should work..

if your forms are modal:

remove the terminate proc in userform2

change userform1 proc to
Private Sub CommandButton1_Click()
Me.Hide
UserForm2.Show
Me.Show
End Sub




--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Peter T wrote :
 
P

Peter T

are your forms Modeless or Modal?

I use both but the example was with modal forms.
if your forms showmodal properties are false
(and thus are shown modeless by default)
then my code should work..

Yes - because with modeless forms code following /show vbmodeless runs on to
the end of the procedure. However I still get the "multiple stacks" of code
I originally referred to.
if your forms are modal:

remove the terminate proc in userform2

change userform1 proc to
Private Sub CommandButton1_Click()
Me.Hide
UserForm2.Show
Me.Show
End Sub

<g> this is exactly the same as I posted in my original example for button1
in UF1.

Look at these debug lines when done. Switch from UF1 to UF2 many times. Exit
and then all those CommandButton1_Click events finally complete. See what I
mean!

'code for userform1
Private Sub CommandButton1_Click()
Me.Hide
Debug.Print "Me.Show start"
UserForm2.Show
Me.Show 'code stops here, after .Show

'only executes on exit
Debug.Print "Me.Show done"
End Sub

(with code in Terminate in UF2 removed)

In reality a lot of code runs before switching forms. Seems like all the
code that led to the switch is left as "sort of" recursive.

Regards,
Peter T
 
P

Peter T

Our previous posts crossed - otherwise wouldn't have needed to clarify the
"growing stack".
However I still get the "multiple stacks" of code
I originally referred to.

Ignore that, it's not correct.

I guess this is normal with MODAL forms. This all came about because I
noticed a small but persistent memory leak (150K) when I switch forms from
code in a popup button. I assumed this was because of the stack issue but
maybe not. The leak is the same if I switch Modal or Modeless forms. I can't
figure it!

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