Page 1 of 2 12 LastLast
Results 1 to 40 of 60

Thread: [RESOLVED] How to exit from VB6 error handler

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Resolved [RESOLVED] How to exit from VB6 error handler

    Hi,
    When an error occurs it is trapped by an error handler which simply terminates by End, exiting from the application.
    In another subject I got the following comment
    Quote Originally Posted by westconn1 View Post
    you have End statement in your error handler, you should avoid using End, unless all your code is just in modules, you should unload all forms, dispose of objects etc and exit gracefully
    Trying to avoid End I now unload the concerned form.
    The problem is now that when the error handler ends the unloaded form goes on and gives an error 364 (Object was unloaded) at the first try to acces one of it's form object !
    How can I end properly ?

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to exit from VB6 error handler

    you could try exit sub (or function) after unloading the form
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: How to exit from VB6 error handler

    Quote Originally Posted by westconn1 View Post
    you could try exit sub (or function) after unloading the form
    Instead of going on in the sub which caused the error
    it exits from the sub which caused the error
    and goes on in the sub which called the sub which caused the error.

  4. #4
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: How to exit from VB6 error handler

    Hi Herve,

    like westconn said use Exit Function or Exit for.

    here a sample to check if the Year exists in a Database.
    In combination with a Boolean and Exit Function/Exit For
    it will check what the User Inputs (a Valid 4-digit Number) and will
    check if the Year already exists in the Database.

    go threw the Function 'EnterYearNumber' and look at the Exit points
    Code:
    Option Explicit
    Private Rs As New ADODB.Recordset
    Dim Cn As ADODB.Connection
    
    Private Sub Command1_Click()
     Dim Msg As String
       Dim Result As String
             If Val(Text1.Text) = 0 Then
                If Not EnterYearNumber Then
                   Unload Me
                   Exit Sub
                End If
             End If
    End Sub
    
    Private Sub Form_Load()
    DoEvents
    sbStatus.Panels.Item(1).Text = "open Database..."
        If (Not openTheDatabase()) Then
            sbStatus.Panels.Item(1).Text = "Database fault..."
            sbStatus.Panels.Item(2).Text = "Error."
        Exit Sub
    End If
    sbStatus.Panels.Item(2).Text = "ready...."
    End Sub
    
    Public Function openTheDatabase() As Boolean
    Dim sConnectionString As String
    On Error GoTo dbError
    sbStatus.Panels.Item(1).Text = "Database open"
    Set Cn = New ADODB.Connection
    sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
                     & "Data Source=" & App.Path & "\Northwind.mdb"
    Cn.Open sConnectionString
    openTheDatabase = True
    Exit Function
    dbError:
    MsgBox (Err.Description)
    openTheDatabase = False
    sbStatus.Panels.Item(2).Text = "Database error..."
    End Function
    
    Private Function EnterYearNumber() As Boolean
       Dim Msg As String
       Dim Titel As String
       Dim IsOk As Boolean
       Dim Result As String
       Dim i As Long
          Titel = "Enter Year"
          Do
             Msg = "please enter the Year..."
             Result = InputBox(Msg, "Year")
             If Len(Result) = 0 Then
                Exit Function
             End If
             IsOk = True
             For i = 1 To Len(Result)
                If InStr(1, "0123456789", Mid(Result, i, 1)) = 0 Then
                   Msg = Result & vbCrLf & vbCrLf & _
                         "is not a valid Number"
                   MsgBox Msg, vbInformation, Titel
                   IsOk = False
                   Exit For
                  ElseIf Len(Result) <> 4 Then
                      Msg = Result & vbCrLf & vbCrLf & _
                         "Year has 4-digits"
                   MsgBox Msg, vbInformation
                   IsOk = False
                   Exit For
                 End If
                 Next
             If ExistYearNumber(Val(Result)) Then
                Msg = "Sorry" & vbCrLf & vbCrLf & _
                      "Year already exists : " & Result
                MsgBox Msg, vbCritical, Titel
                IsOk = False
             End If
             If IsOk Then
                Text1.Text = Result
                EnterYearNumber = True
                Exit Function
             End If
          Loop
    End Function
    
    Public Function ExistYearNumber(YearNr As Long) As Boolean
       Dim Rs As ADODB.Recordset
       Dim sSQL As String
          On Error GoTo Fehler
          Set Rs = New ADODB.Recordset
          sSQL = "Select Year_ID From tbl_Year Where Year_ID = " & YearNr
          With Rs
             .CursorLocation = adUseClient
             .CursorType = adOpenKeyset
             .LockType = adLockReadOnly
             .ActiveConnection = Cn
             .Open sSQL
             If .RecordCount > 0 Then
                ExistYearNumber = True
                Exit Function
             End If
          End With
          Exit Function
    Fehler:
          ShowError Err.Number, Err.Description, "ExistYearNumber"
          ExistYearNumber = True
    End Function
    
    Public Sub ShowError(ErrNumber As Long, ErrDescription As String, _
                             Optional Titel As String = "")
       Dim Msg As String
          Msg = "Error " & ErrNumber & vbCrLf & vbCrLf & _
                ErrDescription
          MsgBox Msg, vbCritical, Titel
    End Sub
    regards
    Chris
    Last edited by ChrisE; Jan 5th, 2018 at 11:48 AM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: How to exit from VB6 error handler

    Quote Originally Posted by ChrisE View Post
    like westconn said use Exit Function or Exit for.
    I think I need to explain a little more.
    I have a main form with 18 buttons; each of them invokes a form.

    This is a sub depending of a control on one of these forms
    Code:
    Private Sub Calcul_Click()
    On Error GoTo Calcul_Click_Error
    ...
    Dessiner "Faisceaux"
    ...
    On Error GoTo 0
    Exit Sub
    
    Calcul_Click_Error:
    
    ErrHdl ("Calcul_Click dans Angles")
    End Sub
    This is the "Dessiner" sub
    Code:
    Private Sub Dessiner(Dessin As String)
    On Error GoTo Dessiner_Error
    ...
    If Sources(srcnr).Color = 0 Then ' This statement causes an error
    ...
    On Error GoTo 0
    Exit Sub
    
    Dessiner_Error:
    
    ErrHdl ("Dessiner dans Angles") ' the error causes the error handler to be called
    End Sub
    Error handler
    Code:
    Public Sub ErrHdl(ByVal ProcMod As String)
    MsgTxt = "Erreur " & Err.Number & " (" & Err.Description & ")" & Chr$(10) & "ligne " & Erl & " procédure " & ProcMod
    
    On Error GoTo ErrHdl_Error ' This to reset the error condition
    
    MsgBox ("Erreur fatale !" & Chr$(10) & MsgTxt "), vbCritical
    ...
    Unload Screen.ActiveForm ' Unload the form which causes the error
    
    ErrHdl_Error:
    Exit Sub ' Gives control to the line after 'Dessiner "Faisceaux"' in the first module, which is unloaded and gives an error 364 (Object was unloaded) as soon as it tries to acces one of it's form object !
    End Sub
    I would like to end the form which caused the error, not the other ones.

  6. #6
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: How to exit from VB6 error handler

    Hi,

    do you know why the error occurs?

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: How to exit from VB6 error handler

    Quote Originally Posted by ChrisE View Post
    do you know why the error occurs?
    Yes, and ?

  8. #8
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: How to exit from VB6 error handler

    Quote Originally Posted by Herve_be View Post
    Yes, and ?
    well I don' really have to know what the error is.

    wish you good Luck
    chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: How to exit from VB6 error handler

    1) I you know what causes an error, perhaps you should be coding to prevent it from happening.
    2) It's hard to tell how to "fix" it because it isn't obvious what is where. You talk about forms, but I don't see any evidence of forms being used or shown in the code.
    3) Depending on the structure of the project, I wouldn't let the error handler (which is really a logger) close the form, I'd let the calling handler handle that.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: How to exit from VB6 error handler

    This has come up recently a couple of times. For what it's worth, here's my 2¢'s worth.

    If it's an error that you might expect (such as possibly opening a database exclusively that someone else also has open), you should possibly give the user options on how to handle things. In this case, you wouldn't want to end the program at all, unless that's what the user decides. And, if the user decides to end, then end as you normally would (close all forms and exit all events).

    If it's an error that you don't expect, then the idea of "defensive programming" comes to mind. IMHO, we shouldn't (and really, can't) attempt to error trap in each/every procedure we write. It's just way too much overhead, and too distracting from getting usable functionality programmed. Therefore, we (or, at least, I) should just try to write as bullet-proof of code as we possibly can, anticipating user garbage input, and handling those situations appropriately.

    And, if we still get an unanticipated error, then, in that particular case, either letting the program crash or executing an "End" statement would possibly be appropriate.

    Again, just my 2¢'s worth.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to exit from VB6 error handler

    i relooked at your original code, as you know where one error occurred, you could try to prevent at least that error from occurring, or at least test the clipboard dataformat to make sure it contains a picture object before trying to save it, this may at least help by reducing the number of errors likely to occur in the error handler
    unloading the form from the procedure within the form (Unload Me), rather than from within the universal error handler, may also resolve the problem, at least worth testing

    i also did not see any code that should continue to run from the original error called from your example textbox_change event, calling init_sources, though even going back to the calling procedure to End Sub, would at least attempt reload the form containing the procedure
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: How to exit from VB6 error handler

    Quote Originally Posted by techgnome View Post
    1) I you know what causes an error, perhaps you should be coding to prevent it from happening.
    Yes of course, that's the first thing I do when an error occurs.
    The problem is that I cannot guess all possible errors : when one occurs I try to collect as much informations as possible to reproduce the case and try to understand why this happens in order to update the code.
    Quote Originally Posted by techgnome View Post
    2) It's hard to tell how to "fix" it because it isn't obvious what is where. You talk about forms, but I don't see any evidence of forms being used or shown in the code.
    My question is not "how to fix the error"; the error is fixed.
    My question is "in case of error how can I neatly terminate".
    Quote Originally Posted by techgnome View Post
    3) Depending on the structure of the project, I wouldn't let the error handler (which is really a logger) close the form, I'd let the calling handler handle that.
    Before, after "logging" the error I simply ended the program : the user had to restart it again.

    Due to a comment from westconn1 in another thread saying "you should avoid using End, you should unload all forms, dispose of objects etc and exit gracefully"
    Trying to avoid End I now unload the concerned form.
    The problem is now that when the error handler ends the unloaded form goes on and gives an error 364 (Object was unloaded) at the first try to acces one of it's form object !
    How can I end properly ?
    Quote Originally Posted by Elroy View Post
    if we still get an unanticipated error, then, in that particular case, either letting the program crash or executing an "End" statement would possibly be appropriate.
    That is precisely what I was doing before the westconn1 comment.
    I'm starting to think that it was ultimately the best solution.
    Quote Originally Posted by westconn1 View Post
    i also did not see any code that should continue to run from the original error called from your example textbox_change event, calling init_sources, though even going back to the calling procedure to End Sub, would at least attempt reload the form containing the procedure
    As the error is unexpected there is no code that should continue to run; the best thing to do is terminate the form which caused the error.
    The only problem is that End terminates also all the other forms, but it was less bad than currently with unload form.
    Last edited by Herve_be; Dec 29th, 2017 at 04:44 AM.

  13. #13
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: How to exit from VB6 error handler

    Hi,

    I don't see anywhere in you code a .. for example: Set Form1 = Nothing

    Code:
    '2 Forms
    '3 commandbuttons in Form1
    Option Explicit
    
    Private Sub Command1_Click()
      Form2.Show
    End Sub
    
    Private Sub Command2_Click()
      Unload Form2
    End Sub
    
    Private Sub Command3_Click()
      DoForm2
    End Sub
    
    Private Sub DoForm2()
      Dim frm As Form
      Dim blnLoaded As Boolean
      'check if Form2 is loaded
      blnLoaded = False
      For Each frm In Forms
        If frm.Name = "Form2" Then
          blnLoaded = True
        End If
      Next frm
      
      'perform action if Form2 is loaded
      If blnLoaded Then
      'do somthing with Form2
        Form2.Caption = CStr(Now)
        Unload Form2
        Set Form2 = Nothing
    'if classes are involved
    'Set Class1 = Nothing
      End If
    End Sub
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: How to exit from VB6 error handler

    Quote Originally Posted by ChrisE View Post
    I don't see anywhere in you code a .. for example: Set Form1 = Nothing
    I've just tried this in my error handler
    Code:
    Unload Screen.ActiveForm
    Set Screen.ActiveForm = Nothing
    this gives "Compile error : invalid use of property".
    I Unload Screen.ActiveForm because I don't know which form is involved.

  15. #15
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: How to exit from VB6 error handler

    Hi,

    without more code it's a guessing game

    how do you load these Forms
    I think I need to explain a little more.
    I have a main form with 18 buttons; each of them invokes a form.
    can you open all 18 Forms ?

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: How to exit from VB6 error handler

    Quote Originally Posted by ChrisE View Post
    how do you load these Forms
    From the main form
    Code:
    Private Sub Form_Load()
    4970  ReDim Prgm(PrgmNum)
    4990  Set Prgm(1) = Salinite
    5010  Set Prgm(2) = CaMgKH
    5030  Set Prgm(3) = Supplementation
    5050  Set Prgm(4) = LogBook
    5070  Set Prgm(5) = Verre
    5090  Set Prgm(6) = Portafo
    5110  Set Prgm(7) = Pompes
    5130  Set Prgm(8) = Surverse
    5150  Set Prgm(9) = Debitmetre
    5170  Set Prgm(10) = KHRAC
    5190  Set Prgm(11) = Angles
    5210  Set Prgm(12) = Luxmetre
    5230  Set Prgm(13) = ColorTemp
    5250  Set Prgm(14) = ICP
    5270  Set Prgm(15) = LEDPower
    5290  Set Prgm(16) = LEDwizard
    5310  Set Prgm(17) = Luminaires
    5330    Set Prgm(18) = Photometre
    When you clic on one of the 18 buttons
    Code:
    Private Sub Programme_Click(Index As Integer)
    Load Prgm(Index)
    Quote Originally Posted by ChrisE View Post
    can you open all 18 Forms ?
    Yes, if you successively clic on all the 18 buttons.

  17. #17
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: How to exit from VB6 error handler

    Hi,

    Yes, if you successively clic on all the 18 buttons.
    do you have to have 18 Forms ? or can you place 18 Frames on one Form ?

    Sample Tabstrip.zip

    see the sample with Tabstrip and Frames


    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: How to exit from VB6 error handler

    Quote Originally Posted by ChrisE View Post
    do you have to have 18 Forms ? or can you place 18 Frames on one Form ?
    It would be very intricate, first because the forms have diffent shapes.
    In fact each form is nearly stand alone, the goal of the main form being to easily activate the form or forms you need.
    Here the main form and some of the sub-forms








  19. #19
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: How to exit from VB6 error handler

    Hi,

    is this a MDI Project with Child forms ?
    how do you Load/Unload the grafics

    one option would be to allow only the Main form and one other Form to be opend.

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: How to exit from VB6 error handler

    Quote Originally Posted by ChrisE View Post
    is this a MDI Project with Child forms ?
    No
    Quote Originally Posted by ChrisE View Post
    how do you Load/Unload the grafics
    I don't load a graphic, I draw it in a picturebox.
    Quote Originally Posted by ChrisE View Post
    one option would be to allow only the Main form and one other Form to be opend.
    No, it is not an option : the user must keep the possibility to open several forms together.

  21. #21
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: How to exit from VB6 error handler

    ok so restating the crux of the problem (of which you seem to have 2).

    Problem 1) the main issue happens when one form has an error worthy of ending the whole program. Because you have multiple forms open, unloading the current form will not end the program.

    Problem 2) even after your error, unloading the current form causes more errors because somewhere in your code you are referring to the now unloaded form.

    just so everyone's one the same page...

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: How to exit from VB6 error handler

    Quote Originally Posted by DEXWERX View Post
    Problem 1) the main issue happens when one form has an error worthy of ending the whole program. Because you have multiple forms open, unloading the current form will not end the program.

    Problem 2) even after your error, unloading the current form causes more errors because somewhere in your code you are referring to the now unloaded form.

    just so everyone's one the same page...
    Not exactly.

    When one form has an error worthy of ending the whole program I ended the whole program and was satisfied with that solution.
    But westconn1 told me that I should avoid using End, I should unload all forms, dispose of objects etc and exit gracefully.
    So I tried to unload only the form which has caused the error, which raised another problem : at the end of the error handler (End Sub) the program continues in the unloaded form which provoques an error as soon as it tries to refer an object of the unloaded form.
    ChrisE suggested to set the unloaded form to Nothing but it gives a compile error.

    I think the best thing to do is to come back to the original version ending the whole program in case of fatal error, I'm sure it works.

  23. #23
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: How to exit from VB6 error handler

    I think you're trying to do too much in your error logger and not enough in your error handler. Your error logger shouldn't be unloading the form or closing the form because the code execution is going to attempt to return to it when it's done.

    Structurally it should (in my opinion) look like this:
    Main Form -> Shows Form1
    Form1 has an Error Handler that calls Error Logger
    Error Logger does what it does, logs the error and notifies the user.
    Control returns to Error Handler.
    Error Handler closes the form it is in.
    Control returns to Main form.
    No errors, no mess, no fuss.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  24. #24
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: How to exit from VB6 error handler

    Quote Originally Posted by Herve_be View Post
    I've just tried this in my error handler
    Code:
    Unload Screen.ActiveForm
    Set Screen.ActiveForm = Nothing
    this gives "Compile error : invalid use of property".
    I Unload Screen.ActiveForm because I don't know which form is involved.
    Hi,
    I think you have to find out which Form is causing this.

    Code:
    Public Sub ErrHdl(ByVal ProcMod As String)
    
       Dim Frm As Form
    MsgTxt = "Erreur " & Err.Number & " (" & Err.Description & ")" & Chr$(10) & "ligne " & Erl & " procédure " & ProcMod
    
    On Error GoTo ErrHdl_Error ' This to reset the error condition
    
    MsgBox ("Erreur fatale !" & Chr$(10) & MsgTxt "), vbCritical
    ...
    Unload Screen.ActiveForm ' Unload the form which causes the error
    'check if it returns the Form that is causing this problem
     Debug.Print Frm
    
    ErrHdl_Error:
    Exit Sub ' Gives control to the line after 'Dessiner "Faisceaux"' in the first module, which is unloaded and gives an error 364 (Object was unloaded) as soon as it tries to acces one of it's form object !
    End Sub
    EDIT:
    how do Unload the Picture(s)

    regards
    Chris
    Last edited by ChrisE; Dec 29th, 2017 at 10:06 AM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  25. #25

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: How to exit from VB6 error handler

    Quote Originally Posted by techgnome View Post
    I think you're trying to do too much in your error logger and not enough in your error handler. Your error logger shouldn't be unloading the form or closing the form because the code execution is going to attempt to return to it when it's done.

    Structurally it should (in my opinion) look like this:
    Main Form -> Shows Form1
    Form1 has an Error Handler that calls Error Logger
    Error Logger does what it does, logs the error and notifies the user.
    Control returns to Error Handler.
    Error Handler closes the form it is in.
    Control returns to Main form.
    No errors, no mess, no fuss.

    -tg
    Just tried to replace the unload form in the error "logger" by unload me in the sub that causes the error,
    same problem than before i.e.
    Code:
    Sub 1
    Call Sub 2
    Code:
    Sub 2
    Makes an error which calls the error Handler than comes back here
    unload me
    End Sub ==> goes back after the call to Sub 2 in Sub 1 : as soon as Sub 1 tries to access an object of its form it gets an error 364	Object was unloaded
    Quote Originally Posted by ChrisE View Post
    I think you have to find out which Form is causing this.
    Code:
     Debug.Print Frm
    I don't understand how you fill Frm
    Last edited by Herve_be; Dec 29th, 2017 at 10:16 AM.

  26. #26
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: How to exit from VB6 error handler

    Hi,

    do you take care to unload the Pictures when you close the Form(s)?

    Code:
    Public Sub ErrHdl(ByVal ProcMod As String)
    
    MsgTxt = "Erreur " & Err.Number & " (" & Err.Description & ")" & Chr$(10) & "ligne " & Erl & " procédure " & ProcMod
    
    On Error GoTo ErrHdl_Error ' This to reset the error condition
    
    MsgBox ("Erreur fatale !" & Chr$(10) & MsgTxt "), vbCritical
    ...
    Set Picture1.Picture = LoadPicture("")
    
    Unload Screen.ActiveForm ' Unload the form which causes the error
    
    
    ErrHdl_Error:
    Exit Sub ' Gives control to the line after 'Dessiner "Faisceaux"' in the first module, which is unloaded and gives an error 364 (Object was unloaded) as soon as it tries to acces one of it's form object !
    End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  27. #27

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: How to exit from VB6 error handler

    Quote Originally Posted by ChrisE View Post
    Quote Originally Posted by Herve_be View Post
    Quote Originally Posted by ChrisE View Post
    how do you Load/Unload the grafics
    I don't load a graphic, I draw it in a picturebox.
    do you take care to unload the Pictures when you close the Form(s)?
    Why should I unload a picture I have not loaded ?

  28. #28
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: How to exit from VB6 error handler

    you are using a Picturebox as an Object to draw something
    how do you clear the Picturebox when you close a Form
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  29. #29

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: How to exit from VB6 error handler

    Quote Originally Posted by ChrisE View Post
    you are using a Picturebox as an Object to draw something
    how do you clear the Picturebox when you close a Form
    Why should I clear the Picturebox ?
    To do this, for each sub associated to each form I would be obliged to clear different pictureboxes.

    Well, I will end the whole program, as before, problem solved, thank you for your help.

  30. #30
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: How to exit from VB6 error handler

    Quote Originally Posted by Herve_be View Post
    Why should I clear the Picturebox ?
    To do this, for each sub associated to each form I would be obliged to clear different pictureboxes.
    Right, because that's the right and proper thing to do... Otherwise you're simply abandoning resources you've occupied and not releasing them back to the system.

    Quote Originally Posted by Herve_be View Post
    Well, I will end the whole program, as before, problem solved, thank you for your help.
    No. I wouldn't say the problem is resolved. You've simply gone back to your previous process which is to simply stick your finger in your ears and ignore the problem.

    You were looking for a one-line, one-size fits all solution to the problem. There isn't one. There never is. Every situation is different, every form is different.

    There might have been a generic way... probably involving loops and the controls collection, but it would require 5-6 lines of code in each form, which you probably wouldn't want so....

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  31. #31

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: How to exit from VB6 error handler

    Quote Originally Posted by techgnome View Post
    You've simply gone back to your previous process which is to simply stick your finger in your ears and ignore the problem.
    What is the problem if I simply quit with End ?
    Isn't it the same as when I clic on the X (close window) ?

  32. #32
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: How to exit from VB6 error handler

    Quote Originally Posted by Herve_be View Post
    What is the problem if I simply quit with End ?
    Isn't it the same as when I clic on the X (close window) ?
    It's closer to bringing up the task manager and ending the process. Who cares about releasing OS Handles or resources.

    note: "ending task" is closer to hitting the X, except it checks if the app responds. Ending the process however is much more abrupt. As tg hinted though - it's pretty clearly a structure issue.

    The nice thing about VB is that everyone can architect an app in an unstructured way. you just end up with problems like this.
    Last edited by DEXWERX; Dec 29th, 2017 at 11:33 AM.

  33. #33

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: How to exit from VB6 error handler

    Quote Originally Posted by DEXWERX View Post
    The nice thing about VB is that everyone can architect an app in an unstructured way. you just end up with problems like this.
    I still don't know which problem.
    When the user wants to quit the application he clics on X; when the application encounters a fatal error it Ends; I never noticed a problem afterwards.

  34. #34
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: How to exit from VB6 error handler

    Quote Originally Posted by Herve_be View Post
    I still don't know which problem.
    When the user wants to quit the application he clics on X; when the application encounters a fatal error it Ends; I never noticed a problem afterwards.
    Unless you see a problem, I wouldn't worry about it then.

  35. #35
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: How to exit from VB6 error handler

    Quote Originally Posted by Herve_be View Post
    Why should I clear the Picturebox ?
    To do this, for each sub associated to each form I would be obliged to clear different pictureboxes.

    Well, I will end the whole program, as before, problem solved, thank you for your help.
    Hi,
    anything you initiate in a Form that you are going to use, you have to (my opinion) Terminate again
    when your finished.

    here a sample of a Form_Unload in one of my Programs

    In the Form I used: more than one Forms, a few Recordsets and a few Classes
    Code:
    Private Sub Form_Unload(Cancel As Integer)
          
       Dim Frm As Form
       'Terminate Forms
          For Each Frm In Forms
             If Frm Is frmZeitModell Then
                Unload Frm
                Set Frm = Nothing
             End If
          Next
          
          'Terminate Classes
          Set cTbox = Nothing
          Set cHelp = Nothing
          Set cADO = Nothing
          Set cPerson = Nothing
          Set cZM = Nothing
          Set cGZ2 = Nothing
          
          'Terminate Resordset(s)
          If RsAn.State = adStateOpen Then
             RsAn.Close
          End If
          Set RsAn = Nothing
          
          If RsZM.State = adStateOpen Then
             RsZM.Close
          End If
          Set RsAn = Nothing
    
         
    End Sub


    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  36. #36

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: How to exit from VB6 error handler

    Quote Originally Posted by ChrisE View Post
    anything you initiate in a Form that you are going to use, you have to (my opinion) Terminate again when your finished.
    Al right, you set all resources to nothing, and what happen then ?
    My problem is that the form being unloaded (I could set all resources to nothing) the code goes on trying to use unloaded resource which leads to an new error.

    When I use the End statement I terminate the current form and also all other loaded forms.
    When I clic on the X only the concerned form terminates : how can I do the same ?

  37. #37
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: [RESOLVED] How to exit from VB6 error handler

    My problem is that the form being unloaded (I could set all resources to nothing) the code goes on trying to use unloaded resource which leads to an new error.
    Then you need to trace through your code and find out why/where/how this is happening. And fix it. I suspect there is more to your forms that just some controls and a few things going on. What do your forms do? Are there backgroundworkers? Threads? Timers? Anything like that?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  38. #38
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: How to exit from VB6 error handler

    Quote Originally Posted by Herve_be View Post
    My problem is that the form being unloaded (I could set all resources to nothing) the code goes on trying to use unloaded resource which leads to an new error.
    but why? that's the obvious design problem. why are you using resources after you've unloaded the form?

  39. #39

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: How to exit from VB6 error handler

    Quote Originally Posted by DEXWERX View Post
    but why? that's the obvious design problem. why are you using resources after you've unloaded the form?
    Already explained twice, for example here #26
    Code:
    Sub 1
    Call Sub 2
    Code:
    Sub 2
    Makes an error which calls the error Handler than comes back here
    unload me
    End Sub ==> goes back after the call to Sub 2 in Sub 1 : as soon as Sub 1 tries to access an object of its form it gets an error 364	Object was unloaded
    Sorry, must go for a while

  40. #40
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: [RESOLVED] How to exit from VB6 error handler

    Your missing the point. Good encapsulation means Sub1 never touches the form.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width