View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
JulieD JulieD is offline
external usenet poster
 
Posts: 618
Default there must be a better way - ensuring userforms are closed

Hi Bob

thanks for your response ...

one example i have of a userform not closing is:

sub userform3_cmdCancel()
unload me
userform5.show
end sub

why would userform3 stayed displayed behind userform5?

Cheers
JulieD

"Bob Kilmer" wrote in message
...
Uh, hate to say this, but the BEST way is to understand why forms are not
closing when you expect them to and make sure you write code that does so.
It is not hard, once you know how. Not saying it is trivial to learn.

Takes
some studying, but almost all of the time, other "solutions" are band-aids
over poor coding practices. You are retaining a reference to a form or

form
control or calling a component on a form, or you are not unloading it when
you think you are. You may be unloading the form, but then calling some
control or property of the form and unknowingly reloading it. Put
debug.print statements in form_initialize and form_terminate event

handlers
with messages like Now() & " loaded form1" and Now() & " unloaded form1"

in
them to keep track of form lifetimes while debugging.

Regards,
Bob

"JulieD" wrote in message
...
Hi All

i have code that should unload a userform when it has finished running

the
code associated with it, but i've found that it doesn't always do it ..

so
i
would like on the initialization of the "menu" userform to ensure that

all
other userforms are closed e.g.

on error resume next
unload userform1
unload userform2

etc

however, i've noticed that when stepping through this code the form
initialization code runs for each of the userforms whether or not they

are
displayed - which slows down the code somewhat.

i know that i can check to see if a userform is visible, and if it is to
close it
e.g.

if userform1.visible = true then
unload userform1
end if
if userform2.visible = true then
unload userform2
end if

but i'm wondering if there is a more efficient way of doing this. (Excel
2000 btw)

Cheers
JulieD