Solved

Definitive Clean Way to Exit App

Posted on 2004-09-12
5
331 Views
Last Modified: 2012-06-21
I currently have an error handler that displays an error message to the user and then executes an 'END' statement. The code is:

Public Sub Error_Handler(fname As String)

  If Err.Number = 53 Or _
     Err.Number = 55 Or _
     Err.Number = 62 Or _
     Err.Number = 75 Or _
     Err.Number = 76 Then
    MsgBox "Error Number: " & Str(Err.Number) & vbCrLf & _
           "Error Description: " & Err.Description & vbCrLf & _
           "File Name: " & fname & vbCrLf & _
           "Program: " & progName & vbCrLf & _
           "Environment: " & envName & vbCrLf & _
           "Eval: " & whichEval & vbCrLf & _
           "Level: " & whichLevel & vbCrLf & _
           Err_Message, vbCritical, "System Error"
  Else
    MsgBox "Error Number: " & Str(Err.Number) & vbCrLf & _
           "Error Description: " & Err.Description & vbCrLf & _
           "Program: " & progName & vbCrLf & _
           "Environment: " & envName & vbCrLf & _
           "Eval: " & whichEval & vbCrLf & _
           "Level: " & whichLevel & vbCrLf & _
           Err_Message, vbCritical, "System Error"
  End If

  End

End Sub

I am running the APP from an executabele (exe). I though that the 'END' statement would completely exit the APP.  In most cases it does but that's not always what happens. Sometimes it take me back to the code as if I was in development mode ( idefiitely don't want the user in the code).  Sometimes it takes me back to a form in the APP that is completely locked.  I have been told that this strange behavior is being caused by the fact that I hce only one 'On Error' in the From load event of a form that had scores of subroutines.

What I am looking for is a definitive 'clean' way to exit the APP unconditonally on these error conditions.  I thought that the 'END' statement was supposed to do that but it is not working as expected.

0
Comment
Question by:mlcktmguy
5 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
There are many unknowns here.

What happens in the Err_Message Sub. Perhaps it loops there or perhaps it has its own call to Error_Handler, for instance?

It is usually not necessary, but many developers make sure that all forms are unloaded before the End statement.

When it displays the code, does it start VB, or was ir running anyway?

Just to check - you are running compiled VB program (Which version, please?) - 'running the APP from an executable' could mean that you are using your executable to run another application.
0
 
LVL 19

Accepted Solution

by:
Shauli earned 125 total points
Comment Utility
Never ever use End in any sub or function, unless it is the last line in the Form_UnLoad event. Using End in any other sub or function can cause problems, such as preventing goog cleanup, and proper close of databases and connections, along with forms that are currently displayed.

So most of us use the Form_QueryUnload event. It allows us to find out why the application is being closed, ie becuase the user chose to, or maybe because the OS is shutting down, etc..

An example would be:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim unForm As Form

Select Case UnloadMode  'why application is being closed
    Case 0    'user ends app
    Case 1    ' app ends by code
    Case 2    'operating system shuts off
    Case 3    'Windows Task Manager is closing app
    Case 4    'MDIform is closing a child form
    Case 5    'a form is closing becuase its owner is closing
End Select

'close all open forms
For Each unForm In Forms
    If Not unForm.Name = Me.Name Then
          unForm.Hide            ' hide the form
          Unload unForm          ' deactivate the form
          Set unForm = Nothing   ' remove from memory
    End If
Next

'close all databases etc.. here
End Sub

===========
So, your code should look like this:

Public Sub Error_Handler(fname As String)

  Select Case Err.Number
     Case 53, 55, 62, 75, 76
       MsgBox "Error Number: " & Str(Err.Number) & vbCrLf & _
           "Error Description: " & Err.Description & vbCrLf & _
           "File Name: " & fname & vbCrLf & _
           "Program: " & progName & vbCrLf & _
           "Environment: " & envName & vbCrLf & _
           "Eval: " & whichEval & vbCrLf & _
           "Level: " & whichLevel & vbCrLf & _
           Err_Message, vbCritical, "System Error"
   Case Else
       MsgBox "Error Number: " & Str(Err.Number) & vbCrLf & _
           "Error Description: " & Err.Description & vbCrLf & _
           "Program: " & progName & vbCrLf & _
           "Environment: " & envName & vbCrLf & _
           "Eval: " & whichEval & vbCrLf & _
           "Level: " & whichLevel & vbCrLf & _
           Err_Message, vbCritical, "System Error"
  End Select

  Unload yourFormName    '<<<<<<<<<<<<<<<<<<<<<<<<<<<<

End Sub


S
0
 
LVL 26

Expert Comment

by:EDDYKT
Comment Utility
>>I though that the 'END' statement would completely exit the APP.  In most cases it does but that's not always what happens.

Strange, you mean your program is still running even it recute the END command?
0
 
LVL 1

Author Comment

by:mlcktmguy
Comment Utility
Thanks for all of the responses, I will first answer the questions asked.

This is my first attempt at VB error handling so I probably made some very important omissions.

The users are running VB6.0 runtime.  The App is in executable form, created from the 'MAKE' exe option on the file menu within the development environment for VB.  The users have no code.

The APP itself is multi-level.  By this I mean that there is a main entry point, frmSelEnv where the user enters a password and other operating options.  If the user selects to do an add of a new element or edit an existing element frmCoverSheet is Opened.  Typically from frmCoverSheet, frmDescription ro frmEvaluation are next.

frmSelEnv is hidden when another form is opened.  Froms are only opened in frmSelEnv.  If the user is in frmDescription and wants to go to frmEvaluation, the needed cleanup and checks are done in frmDescription.  frmDescription set a global variable so that frmSelenv knows what to load next and then frmDesciption is unloaded.  The code in frmSelEnv looks like this:
    Select Case whichForm
    Case "frmDescription"
      whichForm = ""
      frmDescription.Show vbModal
    Case "frmCoverSheet"
      whichForm = ""
      frmCoverSheet.Show vbModal
    Case "frmEvaluation"
      whichForm = ""
      frmEvaluation.Show vbModal
    Case Else
      whichForm = ""
    End Select

Each of the forms has a line in the form load (exept frmSelEnv which has it in the From Activate) event event that looks like this:

  On Error GoTo ErrHandler

at the bottom of the respective event the code is simply:

  Exit Sub

ErrHandler:

  Error_Handler FileName

End Sub

The code for the Error_Handler is listed in my first post.

Yes, I am saying that the APP does not completely close down when it hits the END statement in Error_Handler.  I listed the mish-mash of scenarios that it leaves the user in, in my first post.  My App is not really running but it does not remove itself completely either.  Sometimes I am taken to the 'code' just as if I was in development mode, sometimes it presents partially built screens.

I can see that it would make sense to close all forms before exiting.

I saw the reference to the Query Unload Event.  Does each form have a Query unload event or is that something that only occurs when the entire APP is closing?  
0
 
LVL 19

Expert Comment

by:Shauli
Comment Utility
Each form has its won Query_UnLoad event. However, if frmSelEnv is your main form, then you can point to its Query_Unload and Unload events simply by saying:
UnLoad frmSelEnv

And have the cleanuUp code in frmSelEnv Query_Unload event.

S
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

728 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