?
Solved

Do stuff after the closure of App main form.

Posted on 2003-03-13
15
Medium Priority
?
159 Views
Last Modified: 2010-05-01
I have an App where after I close my Main form I want to call a Sub in a module called 'TerminateApp()' which is to clean up global variables, unload any hidden forms, and then show a form that downloads a database using FTP, and then the App exits.

Is it possible to have the Main form Unload, before TerminateApp executes?

If I do:

frmMain
--------

Private Sub Form_Unload(Cancel As Integer)
   TerminateApp
End Sub


mMain
------

Public Sub TerminateApp()
Dim frm As Form
Dim strOriginalDB As String
Dim strCompactedDB As String
Dim objTSI As New TsiCompact.Jet

   On Error GoTo ErrorHandler

   For Each frm In Forms
      Unload frm
      Set frm = Nothing
   Next
   
   Set cmboCategory = Nothing
   Set cmboSubcategory = Nothing
   
   conn.Close
   Set conn = Nothing
     
   DoEvents
   
   'compact db
   strOriginalDB = App.Path & "\shop.mdb"
   strCompactedDB = App.Path & "\temp.mdb"
   
   
   'clean up temp.mdb from before
   If FileExists(strCompactedDB) Then Kill strCompactedDB
     
   'use the TSI Compacter object....ADO doesn't have a compact method!
   objTSI.CompactDatabase strOriginalDB, strCompactedDB
   
   'kill the original
   If FileExists(strOriginalDB) Then Kill strOriginalDB
     
   'rename
   FileCopy strCompactedDB, strOriginalDB
     
  'clean up temp.mdb from before
  If FileExists(strCompactedDB) Then Kill strCompactedDB
       
   DoEvents
   Sleep 500
   DoEvents
   
   frmSplash.bDownloadMode = False
   frmSplash.Show
   frmSplash.DoTransfer
   DoEvents
   Unload frmSplash
   
   Exit Sub
   
ErrorHandler:
   
   Select Case Err.Number
   

   Case Else
      MsgBox Err.Number & " " & Err.Description
   End Select
End Sub


0
Comment
Question by:holgrave
[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
  • 4
  • 2
  • 2
  • +5
15 Comments
 
LVL 1

Author Comment

by:holgrave
ID: 8127609
...as I was saying....

...if I do the above, frmMain doesn't unload UNTIL TerminateApp has finished executing. Where I want to be able to Unload frmMain BEFORE TerminateApp executes.

Any ideas??
0
 
LVL 8

Expert Comment

by:drydenhogg
ID: 8127657
A form is considered a class within VB, so after the unload event, the terminate event fires.
(Just the same as one of your own classes has a terminate event fire as its last event.)

At this point the form should not be visible to the users, but the code can execute.

ADH.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 8127670
Hi drydenhogg,

Indeed, the Form_Terminate event is fired when all other processing on the form is complete and when the form is being released from memory. It will *NOT* occur if you terminate your application abnormally such as by using the END statement in VB. Using Unload xxx is fine as this will fire the terminate event after the form has been unloaded.

Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com

Experts-Exchange Advisory Board Member
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:drydenhogg
ID: 8127689
Correct Tim, although I would be loathe to use an end statement within any vb app to terminate it.

ADH.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 8127724
I know but you do have to cover all the bases, even though holgrave is almost certainly aware of this, you never know who will be reading this as a PAQ.
0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 1000 total points
ID: 8128082
If you already manage every form to be unlodaded from memory, why don't you simply hide the form execute terminateapp and when the function ends all is pushing down, am i rigth?
0
 
LVL 1

Expert Comment

by:manialok
ID: 8128341
Richie you are right just hide the form and do all stuff
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 8128402
If you start your program from a Sub Main() you can call whatever code you want when the forms are all unloaded.  I have been using Sub Main a lot in programs that have complex initialization-termination requirements as it seems a very clean way to deal with the problem.

0
 
LVL 1

Expert Comment

by:JH0401
ID: 8130430
You could capture the point of unloading the main form by creating your own controls.  Set form Main's control box property to false & add a control to your form that user can click to 'unload the form'.  In this click event you could do the following:

Private sub cmdUnload_Click()

TerminateApp

End Sub

& in your TerminateApp procedure you could do this:

Public Sub TerminateApp()
Dim frm As Form
Dim strOriginalDB As String
Dim strCompactedDB As String
Dim objTSI As New TsiCompact.Jet

  On Error GoTo ErrorHandler

  frmSplash.bDownloadMode = False
  frmSplash.Show
  frmSplash.Visble = False

  For Each frm In Forms
     
Unload frm
     Set frm = Nothing
  Next
 
  Set cmboCategory = Nothing
  Set cmboSubcategory = Nothing
 
  conn.Close
  Set conn = Nothing
   
  DoEvents
 
  'compact db
  strOriginalDB = App.Path & "\shop.mdb"
  strCompactedDB = App.Path & "\temp.mdb"
 
 
  'clean up temp.mdb from before
  If FileExists(strCompactedDB) Then Kill strCompactedDB
     
  'use the TSI Compacter object....ADO doesn't have a compact method!
  objTSI.CompactDatabase strOriginalDB, strCompactedDB
 
  'kill the original
  If FileExists(strOriginalDB) Then Kill strOriginalDB
     
  'rename
  FileCopy strCompactedDB, strOriginalDB
     
 'clean up temp.mdb from before
 If FileExists(strCompactedDB) Then Kill strCompactedDB
       
  DoEvents
  Sleep 500
  DoEvents
 
  frmSplash.Visible = True
  frmSplash.DoTransfer
  DoEvents
  Unload frmSplash

  Exit Sub
 
ErrorHandler:
 
  Select Case Err.Number
 

  Case Else
     MsgBox Err.Number & " " & Err.Description
  End Select
End Sub
0
 
LVL 1

Expert Comment

by:JH0401
ID: 8131430

Private sub cmdUnload_Click()

TerminateApp

End Sub

& in your TerminateApp procedure you could do this:

Public Sub TerminateApp()
Dim frm As Form
Dim strOriginalDB As String
Dim strCompactedDB As String
Dim objTSI As New TsiCompact.Jet

 On Error GoTo ErrorHandler

 frmSplash.bDownloadMode = False
 frmSplash.Visble = False

 For Each frm In Forms
    If frm.Name <> "frmSplash" then
      Unload frm
      Set frm = Nothing
    end if
 Next
 
 Set cmboCategory = Nothing
 Set cmboSubcategory = Nothing
 
 conn.Close
 Set conn = Nothing
   
 DoEvents
 
 'compact db
 strOriginalDB = App.Path & "\shop.mdb"
 strCompactedDB = App.Path & "\temp.mdb"
 
 
 'clean up temp.mdb from before
 If FileExists(strCompactedDB) Then Kill strCompactedDB
   
 'use the TSI Compacter object....ADO doesn't have a compact method!
 objTSI.CompactDatabase strOriginalDB, strCompactedDB
 
 'kill the original
 If FileExists(strOriginalDB) Then Kill strOriginalDB
   
 'rename
 FileCopy strCompactedDB, strOriginalDB
   
'clean up temp.mdb from before
If FileExists(strCompactedDB) Then Kill strCompactedDB
     
 DoEvents
 Sleep 500
 DoEvents
 
 frmSplash.Visible = True
 frmSplash.DoTransfer
 DoEvents
 Unload frmSplash

 Exit Sub
 
ErrorHandler:
 
 Select Case Err.Number
 

 Case Else
    MsgBox Err.Number & " " & Err.Description
 End Select
End Sub
0
 
LVL 1

Author Comment

by:holgrave
ID: 8137142
drydenhogg, Tim C. - I have tried using the Terminate Event - but for some reason it doesn't work!

Instead of using the Unload EVent I have:

Private Sub Form_Terminate()
   TerminateApp
End Sub

...but TerminateApp doesn't execute...???

----------------------------------------------------------------
Richie - Yes I could do this, I could hide the form - BUT it would still mean that frmMain won't be unloaded from memory until TerminateApp finishes excuting - unfortunately TerminateApp can take up to 5 minutes to finish executing (becuase of the FTP stuff) so having frmMain sitting in memory all this time even though it is unneeded is very wasteful of resources - especially as frmMain is v. complex with many controls etc.

I would prefer to have frmMain actually unloaded in some way before TerminateApp executes.

----------------------------------------------------------------

Paul Hews - Yep I use Sub Main to kick my App off. I presume that you are suggesting that in Sub Main I do somthing like:

Private Sub Main()
  frmMain.Show vbModal
  TerminateApp
End Sub

However this then means that frmMain no longer shows in the taskbar! Far from ideal.

Or have I misunderstood? Is there some other way that frmMain notifies the module that Sub Main resides in that it has finished unloading?

------------------------------------------------------------------

JH0401 - I want to handle termination of my app and closure of frmMain when the X is clicked in the top right - your suggestion unfortunately doesn't handle this. To handle the use of the X I have to handle the Unload event and the Terminate event.

I wonder why Sub TerminateApp in the Form_Terminate() event is ignored!!
0
 
LVL 1

Author Comment

by:holgrave
ID: 8142236
drydenhogg, Tim C. - I have tried using the Terminate Event - but for some reason it doesn't work!

Instead of using the Unload EVent I have:

Private Sub Form_Terminate()
   TerminateApp
End Sub

...but TerminateApp doesn't execute...???

----------------------------------------------------------------
Richie - Yes I could do this, I could hide the form - BUT it would still mean that frmMain won't be unloaded from memory until TerminateApp finishes excuting - unfortunately TerminateApp can take up to 5 minutes to finish executing (becuase of the FTP stuff) so having frmMain sitting in memory all this time even though it is unneeded is very wasteful of resources - especially as frmMain is v. complex with many controls etc.

I would prefer to have frmMain actually unloaded in some way before TerminateApp executes.

----------------------------------------------------------------

Paul Hews - Yep I use Sub Main to kick my App off. I presume that you are suggesting that in Sub Main I do somthing like:

Private Sub Main()
  frmMain.Show vbModal
  TerminateApp
End Sub

However this then means that frmMain no longer shows in the taskbar! Far from ideal.

Or have I misunderstood? Is there some other way that frmMain notifies the module that Sub Main resides in that it has finished unloading?

------------------------------------------------------------------

JH0401 - I want to handle termination of my app and closure of frmMain when the X is clicked in the top right - your suggestion unfortunately doesn't handle this. To handle the use of the X I have to handle the Unload event and the Terminate event.

I wonder why Sub TerminateApp in the Form_Terminate() event is ignored!!
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8143838
AFAIK, When terminate event is reached, there is nothing you can do.
0
 

Expert Comment

by:CleanupPing
ID: 8900161
holgrave:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
0
 
LVL 1

Author Comment

by:holgrave
ID: 8904793
In the end I just hid frmMain.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

800 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