Results 1 to 18 of 18

Thread: Does my error handling stink or what?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Does my error handling stink or what?

    Now that I've added this error handling code, when I export to Excel it flashes up on screen then quickly disappears. If I comment out the new code, the Excel export works fine. What gives? This works in Access.

    After I dimmed my variables, I wrote this:
    VB Code:
    1. On Error GoTo CreateExcelSH_Err

    Then at the bottom of the sub I wrote this:

    VB Code:
    1. CreateExcelSH_Exit:
    2.     Call LoadCaption(False)
    3.     For i = 0 To UBound(objRSArray)
    4.         If objRSArray(i).State = 1 Then
    5.             objRSArray(i).Close
    6.         End If
    7.     Next i
    8.     Erase objRSArray
    9.     Erase xlChartArray
    10.     Set xlSheet = Nothing
    11.     Set xlBook = Nothing
    12.         xlApp.DisplayAlerts = False
    13.         xlApp.Quit
    14.         xlApp.DisplayAlerts = True
    15.     Set xlApp = Nothing
    16.     Exit Sub
    17.  
    18. CreateExcelSH_Err:
    19.     Select Case Err.Number
    20.         Case 1004
    21.             MsgBox "Excel cannot draw graphs due to a lack of data.  Please contact the Protection MI team."
    22.             Resume CreateExcelSH_Exit
    23.         Case Else
    24.             MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
    25.             Resume CreateExcelSH_Exit
    26.     End Select

    I'm pretty new at error handling so I really have no idea what I'm doing.

  2. #2

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Does my error handling stink or what?

    Quote Originally Posted by RhinoBull
    I'm not sure which part is new but there is a line tyhat actually closes Excel:

    xlApp.Quit
    Yeah, but I told it to do that only when there's an error...at least that's what I thought I told it to do.

    If I comment out the error-handling code, the report loads with no errors. If I put it back in it falls over, running under the same conditions.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Does my error handling stink or what?

    Am I right in thinking that all of this is for your error handling, and goes after your normal code?

    If so, you need to add an "Exit Sub" before it (and I think it would be easier to read if the CreateExcelSH_Exit: section was after the CreateExcelSH_Err: section).

  5. #5
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Re: Does my error handling stink or what?

    Did you try using breakpoints??
    __________________
    ________________0îîî___
    ___îîî0________(___)____
    __(___)_________) _/_____
    ___\_ (_________(_/______
    ____\_)_________________

  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Does my error handling stink or what?

    Quote Originally Posted by si_the_geek
    If so, you need to add an "Exit Sub" before it .
    That is exactly what's missing (I think) - however - I personally have a major problem using labels in the procedure. Those are true way to create spaghetti code...

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Does my error handling stink or what?

    I don't like labels either - but it's the only way to turn error handling back on for the CreateExcelSH_Exit: part, and that may well be needed.

    I have just noticed something else... xlBook is not closed in that section, and it should be (before it is set to Nothing).

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Does my error handling stink or what?

    Quote Originally Posted by si_the_geek
    I don't like labels either - but it's the only way to turn error handling back on for the CreateExcelSH_Exit: part, and that may well be needed.

    I have just noticed something else... xlBook is not closed in that section, and it should be (before it is set to Nothing).
    OK...I put the 'Exit Sub' before the error handling and that fixed it. Also added xlBook.Close.

    Thanks guys.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Does my error handling stink or what?

    Quote Originally Posted by RhinoBull
    That is exactly what's missing (I think) - however - I personally have a major problem using labels in the procedure. Those are true way to create spaghetti code...
    That's the only way I know how to do it; how else would I write it unless I used labels? I'm not being defensive, I'm genuinely curious.

  10. #10
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Does my error handling stink or what?

    Perhaps like this:
    VB Code:
    1. Private Sub Command1_Click()
    2. '============================
    3. Dim i As Integer
    4. Dim blnExit As Boolean
    5.  
    6. On Error GoTo ErrHandler
    7.  
    8.     i = 10 / 0 'division by zero error will be raised
    9.    
    10.     If blnExit Then
    11.         MsgBox "Exiting due to error."
    12.     Else
    13.         MsgBox "Exiting normally."
    14.     End If
    15.    
    16.     Exit Sub
    17.  
    18. ErrHandler:
    19. '-----------
    20.  
    21.     MsgBox Err.Description
    22.    
    23.     'do this
    24.     blnExit = True
    25.     Resume Next
    26.    
    27.     'or do all your cleanups directly here
    28.     'and exit when it's done instead of resumming
    29.  
    30. End Sub

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Does my error handling stink or what?

    My natural instinct is to remove a few lines like this:
    VB Code:
    1. Exit Sub
    2. CreateExcelSH_Err:
    3.     Select Case Err.Number
    4.         Case 1004
    5.             MsgBox "Excel cannot draw graphs due to a lack of data.  Please contact the Protection MI team."
    6. '            Resume CreateExcelSH_Exit
    7.         Case Else
    8.             MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
    9. '            Resume CreateExcelSH_Exit
    10.     End Select
    11.    
    12. 'CreateExcelSH_Exit:
    13.     Call LoadCaption(False)
    14. ...
    ..but as I mentioned above, this means that error handling will not work on the _Exit section (you will get the standard error messages). This arguably isnt a bad thing, but it depends on whether you think that the _Exit section is likely to have errors.

    In this case, I'd say there is a chance of errors, so using the label method would be useful (ideally with a separate error handler for that section to save 'looping', otherwise just an "On Error Resume Next").

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Does my error handling stink or what?

    Quote Originally Posted by si_the_geek
    My natural instinct is to remove a few lines like this:
    VB Code:
    1. Exit Sub
    2. CreateExcelSH_Err:
    3.     Select Case Err.Number
    4.         Case 1004
    5.             MsgBox "Excel cannot draw graphs due to a lack of data.  Please contact the Protection MI team."
    6. '            Resume CreateExcelSH_Exit
    7.         Case Else
    8.             MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
    9. '            Resume CreateExcelSH_Exit
    10.     End Select
    11.    
    12. 'CreateExcelSH_Exit:
    13.     Call LoadCaption(False)
    14. ...
    ..but as I mentioned above, this means that error handling will not work on the _Exit section (you will get the standard error messages). This arguably isnt a bad thing, but it depends on whether you think that the _Exit section is likely to have errors.

    In this case, I'd say there is a chance of errors, so using the label method would be useful (ideally with a separate error handler for that section to save 'looping', otherwise just an "On Error Resume Next").
    Where could an error happen in the _Exit section?

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Does my error handling stink or what?

    Quote Originally Posted by RhinoBull
    Perhaps like this:
    VB Code:
    1. Private Sub Command1_Click()
    2. '============================
    3. Dim i As Integer
    4. Dim blnExit As Boolean
    5.  
    6. On Error GoTo [B]ErrHandler[/B]
    7.  
    8.     i = 10 / 0 'division by zero error will be raised
    9.    
    10.     If blnExit Then
    11.         MsgBox "Exiting due to error."
    12.     Else
    13.         MsgBox "Exiting normally."
    14.     End If
    15.    
    16.     Exit Sub
    17.  
    18. ErrHandler:
    19. '-----------
    20.  
    21.     MsgBox Err.Description
    22.    
    23.     'do this
    24.     blnExit = True
    25.     Resume Next
    26.    
    27.     'or do all your cleanups directly here
    28.     'and exit when it's done instead of resumming
    29.  
    30. End Sub
    Isn't that a label though?

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Does my error handling stink or what?

    Quote Originally Posted by disruptivehair
    Where could an error happen in the _Exit section?
    Well two possible places are: objRSArray(i).Close and xlApp.Quit

    Isn't that a label though?
    Only for the error handler itself (there is no other option), the extra _Exit label is not needed.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Does my error handling stink or what?

    Quote Originally Posted by si_the_geek
    Well two possible places are: objRSArray(i).Close and xlApp.Quit

    Only for the error handler itself (there is no other option), the extra _Exit label is not needed.
    But wouldn't checking if the recordset was open prior to closing it sort of rule out an error there? You're right about the xlapp.quit being a possible failure point but I don't see how the recordsets could cause problems unless something really weird happens...maybe...hopefully...

    I'm not getting the distinction between labels and things here; I think I probably need to read up more on error handling before diving back in.

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Does my error handling stink or what?

    But wouldn't checking if the recordset was open prior to closing it sort of rule out an error there?
    Possibly not! It depends what state it is in.. if I remember correctly, if you are editing a record at the time then it wont want to close.

    I'm not getting the distinction between labels and things here;
    In your original code you had two labels: "CreateExcelSH_Err:" and "CreateExcelSH_Exit:". Only the _Err one is actually required (to enable the error handling), but the _Exit one is useful in situations like this - where you dont want to return to the 'main' code (as RhinoBulls example does), and your clean-up code may cause errors itself.

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Does my error handling stink or what?

    Quote Originally Posted by si_the_geek
    Possibly not! It depends what state it is in.. if I remember correctly, if you are editing a record at the time then it wont want to close.
    Good point, but I never edit records in those subs...the recordsets are opened as read only anyway.

    In your original code you had two labels: "CreateExcelSH_Err:" and "CreateExcelSH_Exit:". Only the _Err one is actually required (to enable the error handling), but the _Exit one is useful in situations like this - where you dont want to return to the 'main' code (as RhinoBulls example does), and your clean-up code may cause errors itself.
    [/quote]

    OK...I think I'll read up more on error handling anyway, it was a weak spot for me in VBA and continues to be in VB.

  18. #18
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Does my error handling stink or what?

    I'll see if I can explain this a bit more clearly.

    When an error occurs in your 'main' code (after the On Error GoTo..), execution jumps to the label you specified (in this case, CreateExcelSH_Err: ). At that point, you deal with the error however you want (in this case, showing an appropriate message).

    There are basically two options of what to do next - either return to the main code, or do a clean up.

    If you want to return to the main code, there are two main options:
    • One is "Resume", which goes back to re-try the line that had the error (which may have the same error again - RhinoBull's example would).
    • The other is "Resume Next" as RhinoBull showed, which goes to the line after the one that caused the error. You will get past the error, but depending on what the line of code does, this may cause other errors later (if it was opening xlBook, any later code that uses xlBook will fail).
    ..both of these re-enable the error handler, so any further errors that occur will also be handled.

    If you are doing a clean up, there are two main options again:
    • Simply put the clean-up code after the error handler (as in Post #11). Doing this means that the error handler will no longer work - so if the clean-up code has errors, your program will crash.
    • Use "Resume Label" to jump to somewhere else (as you had originally). As with the options above this re-enables the error handler, but there is a problem... any errors that occur in the clean-up will mean that the error handler (and thus the clean-up) will repeat.
    To get around this 'loop' issue (if you think your clean-up might have errors) you should either use a separate error handler for it, or disable error handling for it (using "On Error Resume Next").


    What method you should use depends on the code in the sub/function.. in some cases errors are fixable (eg: if you get a "path not found" you could create the folder and try again), but in others any kind of failure will lead to more errors later (such as the opening of xlBook).

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