Solved

VB6 and Program End

Posted on 2006-10-27
5
273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 66

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 86

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

688 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