Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing dynamic forms 1

Status
Not open for further replies.

krinid

Programmer
Jun 10, 2003
356
CA
How does one
1) detect what (dynamic forms) are present
2) close a dynamic form?

frm.close doesn't work
Unload frm doesn't work either

I have a bunch of dynamic forms created by using the following code which I would like to close without having to make the user hit the "x" button.

Dim frm As frmExistingForm
Set frm = New frmExistingForm
frm.Show vbModeless


Once I figure out how to close them, I need to figure out how to find them. I suppose I could put them in a collection in thisworkbook, but I'd rather detect them if possible.
 
And what about frm.Unload ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
frm does not have that member. I tried using frm defined as (above) an existing form, as UserForm, and as MSforms.UserForm, and none of them had the .Unload member.
 
Anyone know of an API to close/unload a form? I've found some to hide or minimize them, but not to close one.
 
Found it!

Declare Function CloseWindow Lib "user32" (ByVal hwnd As Long) As Long

Now, how can I find a window without knowing it's exact caption? (Or, how can I find out the caption of a window without having it's handle?) I do know the first few characters of the caption, which are unique to the captions of all other windows, if that helps.
 
Correction, under closer examination I found that CloseWindow doesn't actually close windows:
The CloseWindow function minimizes (but does not destroy) the specified window.
(
I verified this by using ShowWindow to bring it back.

Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Which means I can get it out of the user's way, but eventually they'll start getting memory errors.

Back to square one...
 
Hi krinid,

When you say dynamic form what do you mean? Your code snippet indicates that you are using an instance of an already existing form which you should be able to unload. I don't whether being modeless has some sort of effect but it certainly works for modal forms. Can you give a few more details of what happens when you do unload frm - I'm going to have a play.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Tony,
Long time no see! :) You're right - I need more details.

Dynamic in this case means what you pointed out, an instance of an already existing form - many of them. The same code snippet is called multiple times to create the multiple forms.

That said - after finding and fixing a bug in my code, Unload frm appears to work fine now while the frm variable is still in scope, but once the sub ends, it's just an open form with no variables pointing to it.

So I created a collection in ThisWorkbook and added each form as it's created to that collection, which allows me to reference them outside of the sub and Unload them as required.

This works fine now, but I figure there's probably already a collection of active forms out there somewhere that Excel is already keeping track of, so it seems kind of superfluous to be making my own collection.
 
Hi krinid,

There isn't any user-accessible collection of open forms; I'm not sure that even Excel itself particularly needs to keep track of them other than as Windows (they are all owned by the Parent Excel Window).

I have done exactly what you decribe with my own (global) collection of forms when working with recursive structures - the only thing I hadn't done was do it with modeless ones.

Glad you're sorted (and thanks for the star!); letting your object variables drop out of scope is a bit careless, but would explain a multitude of problems [smile]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top