I have confuse between "unload me" and "End" . What is best way to close the program running from executable file?Close the program mean to close the program and clear all the memory used by the program.
Without exception, use "Unload Me", and in the Form_Unload event of each form tidy up as needed (such as closing objects, etc).
Don't use "End", as what that basically does is intentionally crash your program (but without any error messages). None of the code in your _Unload (or_QueryUnload) events will fire, any objects you have open (such as database connections) will not close, etc.
While "End" does some very minimal clean-up, lots of things aren't tidied up - which means your program will probably still be using memory, and might do damage to whatever it was working with.
I am in the process of writing an FAQ article on this subject, but may not have it finished for a few more days.
I used to have something like this (Don't remember exactly)
Code:
Private Sub CloseProgram()
While Forms.Count > 1
Unload Forms(Forms.Count - 1)
DoEvents
End While
Unload Me
End Sub
If you call this code from a form, this loop will keep open that form, so Forms.Count > 1 does the trick. If you're in a procedure like Sub Main and this code is NOT run inside a form then you can also change it to 0.
Actually, I don't know what should I put in the close command button either unload me or end If I running from executable file.
That mean when I close the program, it clear all the memory used for run the program and also the database.
If I used unload me, it is it close all the form including hide form and so on? I want to close all the hiden form and the open up form as well.
If you use "Unload Me", it will unload "me"... So if this is the last form the program will close too. If you have hidden forms, well I'm sorry, that forms will still be there. The code I posted before should do the trick.
Assuming you are using ADO code, you would use code like this for every recordset/command object:
Code:
If (variable.State And adStateOpen) = adStateOpen Then variable.Close
Set variable = Nothing
..and then do the same for the Connection object.
Note that it is best to close any recordset and command objects as soon as you have finished using them, rather than waiting until _Unload.
If you need to keep them open as long as your program is running (like if various parts of the program use the same recordset without re-opening it), then you will need to wait until _Unload.
If consys.State = adStateOpen Or con.State = adStateOpen Then
con.Close
consys.Close
Set consys = Nothing
Set con = Nothing
End If
While Forms.Count > 0
Unload Forms(Forms.Count - 1)
DoEvents
Wend
Unload Me
I'm afraid that this block of code is not good enough:
Originally Posted by matrik02
Code:
If consys.State = adStateOpen Or con.State = adStateOpen Then
con.Close
consys.Close
Set consys = Nothing
Set con = Nothing
End If
First of all, you should not check the .State property of any ADO objects directly against a specific value, as it can have more than one value at a time (such as "open" and "getting data"). Instead, you should use a bit mask like in my example.
Next up, you will get errors if one of those objects is open but the other isn't (as you will try to close the one which is already closed). To deal with it correctly, you should have separate If statements for each one, with just the code to close that particular object.
Finally, you should always set your objects to Nothing.. you cannot do any damage by doing that (even if it has been done already), so put those lines outside the If blocks.
Making those changes means the code would be like this:
Code:
If (consys.State And adStateOpen) = adStateOpen Then
consys.Close
End If
Set consys = Nothing
If (con.State And adStateOpen) = adStateOpen Then
con.Close
End If
Set con = Nothing
..or like this:
Code:
If (consys.State And adStateOpen) = adStateOpen Then consys.Close
Set consys = Nothing
If (con.State And adStateOpen) = adStateOpen Then con.Close
Set con = Nothing
You may find it useful to read this post (from the Database FAQ article "What does this error mean, and how do I fix it?"), which contains a bit more information.