Can someone explain how and when to use the END function please?
Printable View
Can someone explain how and when to use the END function please?
NEVER use END function unless it's completely necessary, because END function slows down your computer every time you use it because it doesn't clean up memory blocks, it just terminates execution of the code completely!!! Chris also said that "Stop" button is about the same if not the same as END, so using STOP button probably isn't good idea either.
The end function is useful for terminating an application that runs in unattended mode and has no forms. I use it to run SQL Server Data import Runs to check that the file I am waiting for has arrived. In an application you should always use the unload event of the form and set your variables and objects to nothing
I still don't see why you should have to use the END function to close a program that has no forms. A program has a logical start and end point
eg.
Sub Main
.
.
.
End Sub
The program may not end at the 'End Sub' because it has other objects that are open - you should really always try to unload / set to nothing all the objects your program is using to close the program cleanly - you're just asking for more problems later on if you use the 'END' command.
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
Well put. End is EVIL
So if I had the following code in each form on the unload event, I would be ok?
'Close connections and clean up memory.
If Not db is nothing then
If (db.State = adStateOpen) then
db.Close ' close connection
set db = nothing ' cleanup memory
End If
End If
Except that you're only going to Set db to Nothing if it's open. Try
If Not db is nothing then
If (db.State = adStateOpen) then db.Close
set db = nothing 'This will execute whether initially open or closed
End If
You should always clean down objects that are owned by the form - so if your 'db' object is private to that form then the unload code would work (Well, you might just want to change it to :
If Not db is nothing then
db.Close
set db = nothing
End if
- as the form is already unloading (hence the unload event) you want to make sure that it cleans up it's objects).
If you've declared the 'db' object as public in the form, then you could be in trouble. If another form / public object in your project has a reference to the 'db' object, then the form will never really be unloaded until all references to that object are cleared.
It's also worthwile doing the following when Unloading forms :
Unload frmSomeForm
set frmSomeForm = nothing
The second line clears down the memory used by VB (it still keeps any static variables in the form in memory when the form has been unloaded). Setting the frmSomeForm to nothing gets around this limitation / 'feature' of VB.
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
Isn't 'Undocumented Feature' the code name for 'bug' at M$?
:)
It is actually documented in the VB documentation (as far back as the original paper manuals - remember them ? - in VB3 I think) - these days you just have to trawl through the MSDN to find out about these 'features'.
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb