VB6 and Program End

Hi,

I have developed a VB6 application that has a form as its 'start up' object.

However the program ends I need it to execute two lines of code, how can I ensure these two lines of code are always executed when the program ends....?

Thanks Ian



LVL 1
ISCAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
There isn't a Global "Application Ending" event in VB6.  (There is in VB.Net 2005 though...)

If you have multiple forms in your application then obviously you won't know which form is the "last one" will you?

Consequently we don't know which form should have the two lines in its Unload() event...

Here is an approach that may work.

From EVERY single forms unload event we call the AppClosing() sub in a Module.  From there we check the Forms collection count if it is 1 then we are closing the last form:

' --------------
'  Form1
' --------------
Option Explicit

Private Sub Form_Unload(Cancel As Integer)
    AppClosing
End Sub

' --------------
'  Form2
' --------------
Option Explicit

Private Sub Form_Unload(Cancel As Integer)
    AppClosing
End Sub

' --------------
'  Module1
' --------------
Option Explicit

Public Sub AppClosing()
    If Forms.Count = 1 Then
        ' the last form is closing...
       
        Debug.Print "The app is closing..."
    End If
End Sub
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Uhh... simply placing the two lines of code before your 'End' command, or before the last form closes, would probably do the job.
0
 
quarkphotonCommented:
please put your code in the "form_terminate" event
0
 
jmundsackCommented:
When you close your main form, the following events will fire:

Form_QueryUnload
Form_Unload
Form_Terminate

The QueryUnload event lets you add code that you want to evaluate to make sure you actually want the form to unload, and a facility to cancel the unload if desired.

The Unload event occurs when the unload was not stopped during the QueryUnload event.

The Terminate event fires after the form has been unloaded, when the COM object that underlies the form is being destroyed.

Where you put your "two lines of code" depends on the content of those two lines.  If they have nothing to do with any of the form's properties (or the properties of any controls on the form), and you want these two lines to be the absolute LAST instructions of your program, I would say put them in Form_Terminate.

HTH-Jon
0
 
ISCAuthor Commented:
Many thanks for all the above comments....

I like the solution provided by 'Idle_Mind' the best....

Thanks Ian
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.