Results 1 to 10 of 10

Thread: [RESOLVED] Unload me, Set Nothing - this is driving me crazy.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Location
    in Poland
    Posts
    390

    Resolved [RESOLVED] Unload me, Set Nothing - this is driving me crazy.

    Hi, Me again
    so will be surely if really I don't find out

    I look through this forum often in search some explicit answer on such question: how to correctly close my project . Obviously, I found a lot of the opinion and with this reason I am very confused.
    It me all muddled up completely.

    I don't know in what case it will suffice to code: only Unload Me, and in what case I have to use additionally Set Nothing.

    I need know how I should close my project in case:

    - 1 Form and several controls
    - several forms and some an controls
    - and when I should use Set Nothing and when I need not use this

    someone could explain

    Thanks in advance
    I know, I know, my English is bad, sorry .....

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Unload me, Set Nothing - this is driving me crazy.

    Tamgovb,

    Look in my signature for Unloading All Forms That should be of some help. But basically if you use
    VB Code:
    1. Set something = something
    You should set the same object to Nothing after you are done using it.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Location
    in Poland
    Posts
    390

    Re: Unload me, Set Nothing - this is driving me crazy.

    Thanks for quick reply.

    Okay, but I don't understand this

    Don't unload active form or we will reload it when we return to it
    Allow active form to unload itself
    However always you unload some active form, right?
    Probably that I'm in great mistake.
    Therefore please about explanation this theme

    Regard
    I know, I know, my English is bad, sorry .....

  4. #4
    Member
    Join Date
    Jan 2006
    Posts
    33

    Smile Re: Unload me, Set Nothing - this is driving me crazy.

    To correctly terminate a vb application you unload all the forms. So if you have 1 form (i.e. form1) which contains several controls on it just execute: Unload form1 statement. Then the rest of the code in that module will execute, the query unload and unload subs will execute and then the program will terminate.

    If you have several forms loaded (though may not be visible) unload them all:
    unload form1, unload form2 etc. If you haven't loaded a form DON'T unload it or the unload fromX will load the form first and then unload it (usually a waste of time and not desired).

    If you have external activex controls it is good practice to terminate the reference to it first i.e. set obj=createobject("Myobject.class") then use a set obj=nothing. If you don't VB will usually do this for you anyway.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Location
    in Poland
    Posts
    390

    Re: Unload me, Set Nothing - this is driving me crazy.

    Thanks for explanation

    Still not all is for me bright, have a look on this :

    VB Code:
    1. For Each frm In Forms
    2.    If frm.Name <> Me.Name Then
    3.        Unload frm
    4.        Set frm = Nothing
    5.    End If
    6. Next
    7. Unload Me
    8. Set frm = Nothing

    VB Code:
    1. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    2.   If UnloadMode = vbFormCode Then
    3.     Msg = "Exiting will stop Scheduled Tasks!"
    4.     If MsgBox(Msg, vbCritical or vbYesNo, "Confirm Exit") = vbNo Then
    5.       'Stop the exit
    6.       Cancel = True
    7.     End If
    8. End Sub

    VB Code:
    1. Private Sub Form_Unload(Cancel As Integer)
    2.     Dim frm As Form
    3.     For Each frm in Forms
    4.         If Not frm Is Me Then Unload frm
    5.     Next frm
    6. End Sub

    VB Code:
    1. Dim frm as Form
    2. For Each frm in Forms
    3.   if frm.hWnd<> Me.hWnd then
    4.     Unload frm
    5.   End If
    6. Next

    VB Code:
    1. Private Sub Form_Unload(Cancel As Integer)
    2.     Dim frm As Form
    3.    
    4.     For Each frm In Forms
    5.         If frm.Name <> Me.Name Then
    6.             Unload frm
    7.         End If
    8.     Next
    9. End Sub

    VB Code:
    1. Dim frm As Form
    2.  
    3. For Each frm In App.Forms
    4.  Unload frm
    5. Next

    How you see every is different (in a little details, but there are), and which it's proper or appropriate. Supposedly are they such the same however the same are not
    Omit details like communiques and MsgBox

    Regard
    I know, I know, my English is bad, sorry .....

  6. #6
    Junior Member
    Join Date
    Apr 2006
    Posts
    22

    Re: Unload me, Set Nothing - this is driving me crazy.

    My dear Tamgovb, actually what r u trying to do..u keep me confusing..

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Location
    in Poland
    Posts
    390

    Re: Unload me, Set Nothing - this is driving me crazy.

    Sorry, I try to understand the sense of code (properly it construction) designed for locking of all forms of application. Presenting these codes I want to get answer on question:
    Why one programmers write Form_Unload and others write Form Query_Unload, and others yet programmers write yet differently.
    What about this decides?
    I know, I know, my English is bad, sorry .....

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Unload me, Set Nothing - this is driving me crazy.

    Quote Originally Posted by Tamgovb
    Don't unload active form or we will reload it when we return to it
    Allow active form to unload itself
    That means that if you unload a form you're going to reference in the future (like there's a textbox on that form and you're going to look at the contents of the textbox after you unload the form), referencing the form (looking at something on it) will reload the form. In that case it's better to hide the form than to unload it and reload it later.

    If you set something, set it to nothing before closing the form you set it on - nothing that doesn't get set to something should get set to nothing. But everything that gets set to something should get set to nothing before you close its form. (I think there's a thread somewhere on vbforums about the memory not being released even after the program ends unless you set the object to nothing.)

    The main point about closing a program is to NEVER Use 'End' to end a program. Close all forms (any of the "For Each frm in Forms" constructs you showed will work, or you can just close each form as you're done with it, if a form is only used once) in the main form's unload event. If something's happening that will cause a problem if the program closes - like saving unsaved entries to disk - put a trap in the form's Form_QueryUnload event. Windows calls that event before closing - those are the prompts you get to save files when Windows is closing. Make sure your objects are all set to nothing. Make sure everything is cleaned up. Form_QueryUnload is your last chance to be smarter than the user.

    Oh, and if you created a record in a database, but the user hasn't put any data into it, Form_QueryUnload is a good place to check the database for empty records and delete them.

    Why one programmers write Form_Unload and others write Form Query_Unload, and others yet programmers write yet differently.
    What about this decides?
    Programming style, personal preference, philosophy. Why do people eat coffee ice cream? I can't stand it. Same reason. The Form_QueryUnload event of every running program is called by Windows when it closes. If you don't need this, use Form_Unload. Then Form_QueryUnload just returns to Windows without doing anything. If you want to check that everything has been cleaned up - data saved, null records deleted, etc., before Windows can close your program - put it in Form_QueryUnload

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Unload me, Set Nothing - this is driving me crazy.

    Quote Originally Posted by Tamgovb
    How you see every is different (in a little details, but there are), and which it's proper or appropriate. Supposedly are they such the same however the same are not
    Omit details like communiques and MsgBox
    The "_QueryUnload" is not unloading - it is just for the user to be able to cancel the unloading (if you want to provide that functionality). This is like in NotePad - if you open it and enter some text, then press the "X" you will get a chance to "cancel", which is basically the same as the example you posted.

    If "_QueryUnload" is not cancelled, the "_Unload" event will then be run - which should contain the code for actually unloading.


    The rest of the examples you posted are essentially the same (to be placed in the "Unload" event - where the form actually closes), except for a couple of points. Some versions dont bother to check if they are unloading the current form (some people say it doesnt matter, but it is "neater" to do it), and the other differences are all about how the check is done.

    The check should not be done based on Name, as you can have multiple forms with the same name (copies of each other). I'm not sure about the "If Not frm Is Me" (I've never used it), but the hWnd version is definitely safe in this respect.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Location
    in Poland
    Posts
    390

    Re: Unload me, Set Nothing - this is driving me crazy.

    Thanks very much, I understand now already this is important theme, at least for me
    I'm satisfied with this answer, I thank again for all and for all answers

    I greet, cheers
    I know, I know, my English is bad, sorry .....

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