Solved

VB6 and Program End

Posted on 2006-10-27
5
261 Views
Last Modified: 2010-04-30
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



0
Comment
Question by:ISC
5 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 17819752
Uhh... simply placing the two lines of code before your 'End' command, or before the last form closes, would probably do the job.
0
 
LVL 2

Expert Comment

by:quarkphoton
ID: 17819774
please put your code in the "form_terminate" event
0
 
LVL 13

Expert Comment

by:jmundsack
ID: 17819793
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 17820704
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
 
LVL 1

Author Comment

by:ISC
ID: 17832251
Many thanks for all the above comments....

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

Thanks Ian
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now