Results 1 to 5 of 5

Thread: Detect Which forms in memory

  1. #1

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    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

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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

  3. #3

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Re: Detect Which forms in memory

    Thanks for the help..

    However, Dim frm As Form is throwing a user defined type error not found...?

  4. #4
    Addicted Member
    Join Date
    Feb 2008
    Location
    Hamburg
    Posts
    138

    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
    Last edited by BManke; Feb 6th, 2008 at 06:05 PM.

  5. #5
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Detect Which forms in memory

    Oops, sorry about that, I completely missed the VBA part. I must have had a 'Senior Moment'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width