VB IDE Crashes When Unloading Form
I am currently using VB6 with SP6 and whenever I use the UNLOAD FORMNAME within the module or even use the following code in a function
Code:
Public Function CloseAllForms()
' Unload and set each form to Nothing (remove it from memory)
Dim OpenForm As Form
For Each OpenForm In Forms
Unload OpenForm
Set OpenForm = Nothing
Next OpenForm
End Function
the IDE crashes. Though when I use this particular function that exists in another program it works ok and I copied the code from my code library in both cases (copy-paste). In one case it accesses access 2000 database and in another it accesses a text file.
I am not sure where to start looking since this code works in some programs but not others and I copied and pasted the code from my code library in ALL cases.
Thanks for any and all help that anyone can offer or suggest.
Re: VB IDE Crashes When Unloading Form
Well one change that most of our senior members will agree on, is
Code:
Public Function CloseAllForms()
' Unload and set each form to Nothing (remove it from memory)
Dim OpenForm As Form
For Each OpenForm In Forms
If OpenForm.HDC <> Me.HDC Then
Unload OpenForm
Set OpenForm = Nothing
End if
Next OpenForm
Unload Me
End Function
Gremmy...
Re: VB IDE Crashes When Unloading Form
Quote:
Originally Posted by DaWolf
I am not sure where to start looking since this code works in some programs but not others and I copied and pasted the code from my code library in ALL cases.
Thanks for any and all help that anyone can offer or suggest.
First step would be to add the code as Gremmy shows so you don't call Unload on the form in the middle of your function. Next is to debug each form unloading.
Put a breakpoint on the line with the Unload call and run your application and quit like you normally do when it crashes. It should hit the breakpoint and you should be able to step with F8 and enter the Unload event of the form it told to unload.
If you step through each Unload event and your IDE suddenly crashes, you should have a good idea on what could be causing it, or at least which Form is causing a problem.
Oh, and the line Set OpenForm = Nothing in the For loop isn't needed.
Re: VB IDE Crashes When Unloading Form
Oop's .. i didn't notice that, I just copy pased the OP's code and did a quick edit.. But your right about Set Form = nothing not been needed...
The rest of your post is spot on, very good debuging advice...
Re: VB IDE Crashes When Unloading Form
Well, I just noticed that it's a Public Function, so maybe it's located in a module and not a form, in which case there is no Me object so the check that you're not unloading wouldn't work.
Unless you passed the form object ByRef to it:
Code:
Public Function CloseAllForms(ByRef CallingForm As Form)
' Unload and set each form to Nothing (remove it from memory)
Dim OpenForm As Form
For Each OpenForm In Forms
If OpenForm.HDC <> CallingForm.HDC Then Unload OpenForm
Next OpenForm
Unload CallingForm
End Function
And I just got curious. Why check the hDC? I would generally check or compare by the hWnd.
Not that it doesn't work with hDC, but I'm just curious.
Re: VB IDE Crashes When Unloading Form
Quote:
Originally Posted by ChaosTheEternal
And I just got curious. Why check the hDC? I would generally check or compare by the hWnd.
Not that it doesn't work with hDC, but I'm just curious.
Well,i remeberd it was "H" Something, and HDC was first on intelisence. And i think someone also posted this code with hDC before.....
But like you said .. it will work..
BTW - I see you also Copy Pasted !!!
Re: VB IDE Crashes When Unloading Form
Thanks to all for your help. I changed the code as GremlinSA suggested and I no longer had the IDE crashing. I am not sure what the original cause was but it appears to be resolved at this time.
Again thanks to everyone