Detect Which forms in memory
Hey all,
I realize this has probably been asked before, but I'm having trouble finding the answer. Is there some code which can detect if a certain form is in memory (ie has been loaded but not unloaded)? Then from there I would want to unload it...
Sorry for most likely a repeat question, but thanks for the help!
Pg
Re: Detect Which forms in memory
This will enumerate all loaded forms to the Immediate Window:
Code:
Dim frm As Form
For Each frm In Forms
Debug.Print frm.Name
Next
This type of code is used to make sure that all forms have been unloaded prior to closing an applications. eg:
Code:
Dim frm As Form
For Each frm In Forms
If frm.Name <> Me.Name Then
UnLoad frm
End If
Next frm
Unload Me
Re: Detect Which forms in memory
Thanks for the help..
However, Dim frm As Form is throwing a user defined type error not found...?
Re: Detect Which forms in memory
because form does not exist in vba.
you are doing this in excel or word or something, right? vba is different from VB here.
you can do:
dim frm as userform
the appropriate collection would be userforms rather than forms.
unfortunately it does not support the .name property and the caption property does not work as it should - so what you want to do is a bit hard. The only workaround i can think of now would be to check the userforms controls collection for a certain control by which you can identify it (e.g. by a textbox of which you know the name like txtMyBox or something) and which is unique to that userform...
can't you clean up the code to ensure forms do not go into a limbo state like this?
The code below shows how to reference the broken caption property.
(Oh an other workaround that could work: make every form set its caption in the initialize event. that should access the same thing as the userform.caption thingy.)
Code:
Sub test()
Dim frm As UserForm
frmStart.Show vbModeless
For Each frm In UserForms
debug.print frm.caption
Next frm
End Sub
Re: Detect Which forms in memory
:blush: Oops, sorry about that, I completely missed the VBA part. I must have had a 'Senior Moment'