Solved

VB6 and Program End

Posted on 2006-10-27
5
255 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

747 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

13 Experts available now in Live!

Get 1:1 Help Now