-
What is the defference between END and Unload form1. I here so many complaints about which one to do but never a good resaonable explanation about which one is better to use. So one day a long time ago I heard someone tell me to use unload and another to end so what I did was put this code in the module.
Code:
Sub killapp()
unload form1
unload form2
unload form3
unload form4
end
end sub
Is that better then using them both singaly or what should everyone be using?
Thanks!!
-
I like this better:
Code:
Private Sub Form_Unload(Cancel As Integer)
Dim Form As Form
For Each Form In Forms
Unload Form
Next Form
End Sub
then you just go
and your whole program will end.
-
But why use Unload over end? I don't understand why unload is said to be better then End.
-
End is to end the entire project. When you use END, it automatically unload all forms along with it.
Unload is to unload ONE form and not all forms. Of course, if you unload all forms, the last one will END your program.
-
Using the end doesn't free up all the memory that your program uses. After you unload your forms it is also a good idea to 'Set form = nothing'.
This is just a memory thing. You need to clear it up once the program is done.
-
Actually, End differs from Unload and I recommend against using it simply because when you use End instead of Unload, none of the unload events (QueryUnload, Unload, Terminate) are triggered whereas Unload will trigger all of them.
-
and Unload triggers the unload event of the form. if you use end it doesnt, this can be a problem when your subclassing and have the unsubclass sub called in the unload event.
-
Ok what happens if you use both end and unload. Will it free up the memory and work all perfect like.
Code example from Denniswrenn but modified
Code:
Private Sub Form_Unload(Cancel As Integer)
Dim Form As Form
For Each Form In Forms
Unload Form
Next Form
End
End Sub
Is that the correct and proper way to do it?
-
That won't make very much difference.... Because it will end even if you have 50 forms open with my original code...
-
Ok thanks. That clears everything up for me. And thank you gain everyone!!!
-
Think of it this way. Using End is like hitting the stop button on VB and using Unload is like clicking the X on your app.
-
My 2 cents...
Always unload your forms. If you do this, using the End statement is not required.
I created an app or two before being aware of the importance of unloading forms, using only the End statement to stop the program. Although my app was terminated, it would still be visible as a running program when I pressed CTL-ALT-DEL. When I started unloading my forms to end my apps, this problem went away.
I just wanted to point this out because I didn't see it mentioned in the previous posts and this is a visible way to see a difference between the two methods.
All the best.
-
You should also set your forms to nothing to free up resources, as thedee23 said. So, adding a bit to Dennis's code:
Code:
Public Sub UnloadAllForms()
Dim Frm As Form
For Each Frm In Forms
Unload Frm
Set Frm = Nothing
Next Frm
End
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnloadAllForms
End Sub
And overall, I'd use both the Unload and End statements.
-
So are you basically saying (GURU) that we should avoid using End where possible. I Unload every form after a new form is required. Then when someone clicks exit from any form I just use END. Does this mean that my resources arent being freed. Would the Process viewer in Visual Studio Tools show resources being hogged up due to forms that werent unloaded?
BTW...how do u put the VB code into this thing?
-
Just a quick warning about the 'End' statement.
My company had an application with a rogue 'End' in it. When you use the VBSQL control to access MS SQL Server, you can get into all sort of trouble if you do not close down your connections in the correct order (it will GPF on app exit).
I have seen this with other controls as well, so you are best not using 'End' until you have manually unloaded your whole app. In fact as everyone else has said, just don't use 'End' your app should be keeping track of what is unloaded and not.
We only use the 'End' statement in one situation, when the app has realised that all hell has broken loose and it should quit as fast as possible.
Everyone else seems to have covered this, but just thought I'd post what can go wrong using 'End'.
-
I've tried unloading all forms, but I'm still hanging up when I exit my app. My app has a number of forms that allow the user to go 'back' or 'next'. I show a form, then hide the current form when they click on 'back' or 'next'. After the current form is hidden I show the new form and keep doing this until all forms have been displayed and 'next' is clicked one last time. My app runs fine if the user never clicks 'back'.
Thanks in advance for your replies.