Results 1 to 5 of 5

Thread: Program termination

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Program termination

    In the app's main form's Unload event I have this code:
    Code:
        For Each Frm In Forms
            Set Frm = Nothing
        Next
    Now, there's a button in the main form that when clicked on shows a second form in modal style:

    frm2.Show 1

    If I close this form hiding it instead of unloading it, then when the above Unload code runs the program does not terminate (I'm working in the IDE) so I have replaced frm2.Hide by Unload Me (frm2) and now program ends corrcetly.

    The question is, if there wrere reasons for using frm2.Hide instead of Unload frm2, could I place the unload statements in the main form's unload event like this?

    Code:
        For Each Frm In Forms
            Unload Frm    'Is this OK?
            Set Frm = Nothing
        Next
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,746

    Re: Program termination

    Code:
    For Each Frm In Forms
       Set Frm = Nothing
    Next
    Doesn't do a thing.
    The new variable is set to the existing object returned from the Forms collection.
    It's not the actual variable, Frm is just a second reference to the actual form variable
    Setting Frm to Nothing will only release Frm, the original object still exists.

    Code:
    For Each Frm In Forms
      Unload Frm ' That's OK
      ' Set Frm = Nothing 
    Next

  3. #3

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Program termination

    Quote Originally Posted by Arnoutdv View Post
    Code:
    For Each Frm In Forms
       Set Frm = Nothing
    Next
    Doesn't do a thing.
    The new variable is set to the existing object returned from the Forms collection.
    It's not the actual variable, Frm is just a second reference to the actual form variable
    Setting Frm to Nothing will only release Frm, the original object still exists.

    Code:
    For Each Frm In Forms
      Unload Frm ' That's OK
      ' Set Frm = Nothing 
    Next
    However, I have inserted a Debug.Print Frm.Name at the start of the above loop and it correctly prints the corresponding form name, as long as the form is still loaded (for example, hidden). In the case I have form1 and form2 but unload form2 prior to unloading form1, only form1 is set to nothing in the loop.

    So generally speaking, the For Each Frm In Forms loop references all forms still loaded but does not unload them. I now wonder what this loop actually does or if forms are implicitly set to nothing as they are unloaded so the Set Frm = Nothing statement is unnecessary. As a matter of fact, I copied the loop sequence from someone's post in these forums.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  4. #4
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Program termination

    The For Each Frm In Forms loop references all forms still loaded, but does nothing with those forms unless you include code to do something. The Set Frm = Nothing statement is unnecessary because the Frm variable is only a reference, not the form itself. Properly, you need to unload each form except the form that the code is in.

    Code:
    For Each Frm In Forms
      If Not Frm Is Me Then Unload Frm
    Next
    Presumably, this code is in the unload event, so the 'Me' form will be unloaded upon completion. I believe the syntax is correct. This has certainly been discussed before, and may even be in the FAQ section. Indeed, it is.
    Last edited by Logophobic; Nov 20th, 2013 at 09:06 AM. Reason: Link to FAQ

  5. #5

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Program termination

    Quote Originally Posted by Logophobic View Post
    ...This has certainly been discussed before, and may even be in the FAQ section. Indeed, it is.
    I think I get it now. Your link points to a nice tutorial, thanks.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

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