Results 1 to 8 of 8

Thread: Finding Child Forms Closing Point

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    11

    Finding Child Forms Closing Point

    hi everyone,
    I am using a MDI form which contains many childs. I want to execute a certain code "only if the child is individually close". And I dont want that executed if the main MDI form is closed. I tried putting a boolean global variable in the MDI_Unload and MDI_Terminate events. And checking the boolean at the form_unload event of child, still the code executes.
    Example:

    Private Sub MDIForm_Terminate() 'In Main Form
    CloseAllTabs = True
    End Sub

    Private Sub Form_Unload(Cancel As Integer)
    If Not CloseAllTabs Then MsgBox Me.Caption
    End Sub

    the message box pops up everytime. Even if MDIForm is closed. Help me.
    thanks in advance.

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,177

    Re: Finding Child Forms Closing Point

    Deleted
    Last edited by SamOscarBrown; May 30th, 2013 at 12:42 PM. Reason: Deleting

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Finding Child Forms Closing Point

    Assuming CloseAllTabs boolean variable is defined in the module...try the following.
    Instead of MDIForm_Terminate use MDIForm_QueryUnload event handler:

    Code:
    'mdi form
    Private Sub MDIForm_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        CloseAllTabs = True
    End Sub
    
    'child form
    Private Sub Form_Load()
        CloseAllTabs = False
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        If Not CloseAllTabs Then
            Debug.Print "Closing MDI Child form..."
        Else
            Debug.Print "Closing MDI form..."
        End If
    End Sub
    To see in what sequence events are triggered place Debug.Print <event name> in various events, then run and close the form.

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Finding Child Forms Closing Point

    If you do that then you should get these results:
    Code:
    Private Sub MDIForm_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        Debug.Print "MDIForm_QueryUnload"
    End Sub
    
    Private Sub MDIForm_Unload(Cancel As Integer)
        Debug.Print "MDIForm_Unload"
    End Sub
    
    Private Sub MDIForm_Terminate()
        Debug.Print "MDIForm_Terminate"
    End Sub
    
    'results:
    MDIForm_QueryUnload
    MDIForm_Unload
    MDIForm_Terminate
    As you can see Terminate event is triggered last and after all child forms are closed - this is basically why your CloseAllTabs flag is still False.

  5. #5
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Finding Child Forms Closing Point

    Use the event QueryUnload where you can know what cause the form close, e.g.

    Code:
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        If UnloadMode = vbFormMDIForm Then
            MsgBox "My MDI parent is closing"
        ElseIf UnloadMode = vbFormControlMenu Then
            MsgBox "Just me is closing"
        ' Add extra check for other UnloadMode (if you need)
        End If
    End Sub



  6. #6

  7. #7
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Finding Child Forms Closing Point

    Quote Originally Posted by RhinoBull View Post
    @4x2y:

    did you even read post #3 before posting?
    Sorry, i didn't intended to repeat your solution because i saw it after posting, unfortunately this is frequently happens.


    Edit:
    After reading your previous posts thoroughly, i detect that i didn't repeat because you are using the MDI's QueryUnload event while i'm using the child form's QueryUnload so there isn't any reason for a global variable.
    Last edited by 4x2y; May 29th, 2013 at 08:42 AM.



  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    11

    Re: Finding Child Forms Closing Point

    thank you so much Rhinobull and 4x2y, it helped

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