Results 1 to 4 of 4

Thread: Form_Terminate vs Form_Unload?

  1. #1

    Thread Starter
    Hyperactive Member Al Smith's Avatar
    Join Date
    May 1999
    Location
    Marcellus, MI. USA
    Posts
    330
    Hi,
    What does the Form_Terminate event do versus the Form_Unload event?
    And when should you use one or the other?
    Thanks,
    Al.

  2. #2
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523
    From MSDN:
    UNLOAD
    Occurs when a form is about to be removed from the screen. When that form is reloaded, the contents of all its controls are reinitialized. This event is triggered by a user closing the form using the Close command on the Control menu or an Unload statement.

    Syntax
    Code:
    Private Sub object_Unload(cancel As Integer)
    The Unload event syntax has these parts:

    Part Description
    Object Anobject expression that evaluates to an object in the Applies To list.
    Cancel Integer that determines whether the form is removed from the screen. If cancel is 0, the form is removed. Setting cancel to any nonzero value prevents the form from being removed.


    Remarks

    Setting cancel to any nonzero value prevents the form from being removed, but doesn't stop other events, such as exiting from the Microsoft Windows operating environment. Use the QueryUnload event to stop exiting from Windows.

    Use an Unload event procedure to verify that the form should be unloaded or to specify actions that you want to take place when the form is unloaded. You can also include any form-level validation code you may need for closing the form or saving the data in it to a file.

    The QueryUnload event occurs before the Unload event. The Unload event occurs before the Terminate event.

    The Unload event can be caused by using the Unload statement, or by the user choosing the Close command on a form's Control menu, exiting the application with the End Task button on the Windows Task List, closing theMDI form for which the current form is a child form, or exiting the Microsoft Windows operating environment while the application is running.

    TERMINATE
    Occurs when all references to an instance of a Form, MDIForm, User control, Property Page, Webclass, DHTML Page Designer, or class are removed from memory by setting all the variables that refer to the object to Nothing or when the last reference to the object falls out ofscope.

    Syntax
    Code:
    Private Sub object_Terminate( )
    The object placeholder represents anobject expression that evaluates to an object in the Applies To list.

    Remarks

    For all objects except classes, the Terminate event occurs after the Unload event.

    The Terminate event isn't triggered if the instances of the form or class were removed from memory because the application terminated abnormally. For example, if your application invokes the End statement before removing all existing instances of the class or form from memory, the Terminate event isn't triggered for that class or form.


    HTH


  3. #3

    Thread Starter
    Hyperactive Member Al Smith's Avatar
    Join Date
    May 1999
    Location
    Marcellus, MI. USA
    Posts
    330

    Yes. I know. I read this but...

    I still don't understand the difference.
    My question asises because I'm playing around with the MSChart sample in VB6. It doesn't have a form_unload. Instead it has a File > Exit in the menu. This calls:
    Code:
    Private Sub mnuExit_Click()
        Unload Me
    End Sub
    Does this perform the same function as Form_Unload?

    There is also the following:
    Code:
    Private Sub Form_Terminate()
        Cleanup
    End Sub
    Public Sub Cleanup()
        shtGas.Close
        Set shtGas = Nothing
        Set rngGas = Nothing
        If ExcelWasNotRunning = True Then
        appGas.Quit
        End If
        Set appGas = Nothing
    End Sub
    Couldn't all this be in the Form_Unload event?
    Thanks again,
    Al.


  4. #4
    Addicted Member pardede's Avatar
    Join Date
    Jan 2000
    Posts
    232
    Think of the form as consisting of 2 parts, the object part (the 'physical' form) and the code part (any sub/function you code in it).

    UNLOAD unloads the the object part of the form from the memory. But the app still keep a reference to this form (in an intrinsic global variable with the same name as the form). In this state, the code part of the form is still in the memory.

    TERMINATE 'destroys' the form completely. This happens when the reference to the form is removed (e.q. by Set Form1 = Nothing). After this also the code part of the form is removed from memory.

    So the sequence is:
    INITIALIZE - LOAD - UNLOAD - TERMINATE

    And yes, i think your code can be put in the unload event.

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