Click to See Complete Forum and Search --> : Using End statement and Unhook without gpf


nickwa
October 18th, 2001, 06:34 PM
Hi, I am doing some subclassing. I know that I must unhook before I use the End statement. But If I unhook then on next line the End statment is used to end the program it gpfs! I assume it has not fully unhooked. If I have two buttons one that unhooks and one the has the End statment, and I press the unhook button then the end button program shutsdown ok, I assume that it has had time to fully unhook. But this is not a professional way to end a program! ie tell users to shut a program down press this one button then the other one! It does close ok if the X in the top right hand corner is used however. I assume the X in the top right hand corner must handle the shut down of the program differently.

Thanks for your help! I assume that this question is really a beginers question.

John G Duffy
October 18th, 2001, 06:52 PM
You should never use the END statement except in the Form_Load event of the application startup form. So says Microsoft. END will terminate all program execution with absolutely no cleanup work. FIles will be left open, DataBase connections left open, memory allocations will remain causing memory leaks and all sort of other ugly problems.
Terminating a application in a system routine is alomst guaranteed to cause problems.
To shutdown your program properly, use the Unload statement. Then in the unload event, do something like this to cause all open forms to close.

option Explicit

private Sub Command1_Click()
Unload me
End Sub

private Sub Form_Load()
Load Form2 ' load some forms as a example
Load Form3
End Sub

private Sub Form_Unload(Cancel as Integer)
Dim f as Form
for Each f In Forms
If f.Caption <> me.Caption then Unload f
next f
End Sub



Ths form unload event causes cleanup processing to occur. You may need to do some of your cleanup work such as unallocating memory you explicitely allocated or updating databases with that last record etc.
The routine above will cause the unload event of all open forms to execute so they can clean up their mess.

John G