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!
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.
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.
Okay, when the user clicks on your button...
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...
Private Sub CommandUnload_Click()
MyLoopFlag = True
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...
Then you would want to include this code after the F.Show
Dim F As Form
Set F = New Form1
F.Show vbModal, Me
Hope I explained that well enough...
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?
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
Last Post: 02-23-2006, 07:08 PM
Last Post: 02-02-2006, 04:42 AM
By maheeru in forum VB Classic
Last Post: 08-09-2005, 12:59 PM
By skyxmen in forum VB Classic
Last Post: 09-04-2001, 02:51 PM
By Setya in forum VB Classic
Last Post: 01-09-2001, 03:57 AM
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL