DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Completely closing out a form ...

  1. #1
    Join Date
    Apr 2004

    Question Completely closing out a form ...

    VB6/WinXP/Hobby Programmer. I have traced several recent issues I've been having with my app to one problem in code. My app is waiting for input from the User in a loop, where I have several DoEvents statements (per suggestions here). I have discovered that when the User closes out of the App using the Close X on the form while this loop is running the form never gets out of the loop and hence never completely unloads (creating future problems).

    When the user goes back to a directory form they can click on other choices then come back to this form as on option, which then loads another instance of this form while that first loop is still waiting for input (sometimes syncing up, but usually creating an eventual crash).

    I realize I need additional code to clean up my exit from the form when the X is clicked (when the user leaves properly using a command button they are already outside of the 'request input loop' so it unloads the form properly). My question: Does VB provide some command that will end all running Subs (such as this one waiting for input), when the form is closing (and the User's input is no longer relevant to this instance)? Or, how can I cleanly escape that loop when the user doesn't actually provide any more input than to click the form X Close command?

    Any suggestions or advice is greatly appreciated!

  2. #2
    Join Date
    Jun 2009
    I won't go into asking why you are using a loop to wait upon user input as there many events that will capture user input but I will say that you should use the forms query unload event to set a form level variable to T/F your choice that you can test for inside of your loops and if the boolean variable = T/F then exit for/do/loop.

    Good Luck

  3. #3
    Join Date
    Apr 2004
    Thanks, VB, that worked. Long story on the loop. I hate the loop, but it seems to be the only way for me to accomplish what I'm trying to do. Maybe I will post for alternatives if I can get my thoughts together long enough to clearly explain what I'm trying to accomplish. That's another day.

    I actually spent the whole day working on this form unloading issue and think I have a much better grasp now of the whole process (and where that 'Close' button fits in). It's working well now.

    I do still have one question you might be able to help with. Whether using my command button to leave the form, or the Close button, the Terminate event is not firing. Might you have a guess as to why that might be? And is it inappropriate to put my 'Set form=nothing' statement in the Unload event instead?

    Thanks for help.
    Last edited by Shannon; 08-29-2009 at 01:48 AM.

  4. #4
    Join Date
    Jun 2009
    Okay, when the user clicks on your button...
    Private Sub CommandUnload_Click()
    MyLoopFlag = True
    Unload Me
    End Sub
    The unload statement is the only thing you need to have the form unload correctly. When that line of code is reached the following events happen...

    Query Unload

    Query Unload is where you test to see how your program is ending and in some cases you can stop your form from unloading. (You can't stop it when OS tells program to close and query unload does not catch being ended by task manager.)

    Unload is where you clean up any visual objects you created at runtime and most people also use it to clean up other objects like ADO connections and the like.

    Terminate is the last chance to clean up non visual elements (ADO for example).

    Now, if the above form was call with code like this...
    Dim F As Form
    Set F = New Form1
    F.Show vbModal, Me
    Then you would want to include this code after the F.Show
    Set F = Nothing
    Hope I explained that well enough...

    Good Luck

  5. #5
    Join Date
    Apr 2004
    So when I just load a form normally, from another form ... when I'm done with it and use the unload statement, I don't need the 'Set Form2=nothing' line. It's not necessary. Is that correct?

  6. #6
    Join Date
    Apr 2007
    Sterling Heights, Michigan
    That is correct.

Similar Threads

  1. Replies: 0
    Last Post: 02-23-2006, 08:08 PM
  2. Program crash when closing a form
    By Narri in forum C++
    Replies: 3
    Last Post: 02-02-2006, 05:42 AM
  3. Replies: 3
    Last Post: 08-09-2005, 12:59 PM
  4. Replies: 2
    Last Post: 09-04-2001, 02:51 PM
  5. Setting form as MDIChild form at runtime
    By Setya in forum VB Classic
    Replies: 4
    Last Post: 01-09-2001, 04:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center