Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 160
  • Last Modified:

Do stuff after the closure of App main form.

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
holgrave
Asked:
holgrave
  • 4
  • 2
  • 2
  • +5
1 Solution
 
holgraveAuthor Commented:
...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
 
drydenhoggCommented:
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
 
TimCotteeCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

ADH.
0
 
TimCotteeCommented:
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
 
Richie_SimonettiIT OperationsCommented:
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
 
manialokCommented:
Richie you are right just hide the form and do all stuff
0
 
PaulHewsCommented:
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
 
JH0401Commented:
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
 
JH0401Commented:

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
 
holgraveAuthor Commented:
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
 
holgraveAuthor Commented:
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
 
Richie_SimonettiIT OperationsCommented:
AFAIK, When terminate event is reached, there is nothing you can do.
0
 
CleanupPingCommented:
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
 
holgraveAuthor Commented:
In the end I just hid frmMain.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 4
  • 2
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now