PDA

View Full Version : Error handling



Codeblue
06-29-2012, 06:26 AM
I have an app that is prone to Run-time error 1004.
I saw this code...

On Error Goto ErrHandler:
N = 1 / 0 ' cause an error
'
' more code
'

Exit Sub
ErrHandler:
' error handling code
Resume Next

I know what needs to be done for this error the user needs to log onto a web site and open an excel file. What is a good approach to capture this error and give instruction via a msgbox?

CatDaddy
06-29-2012, 09:21 AM
msgbox(Err)
?

Codeblue
06-29-2012, 09:50 AM
I put this in

Exit Sub
ErrHandler:
MsgBox ("You must log into EMaint and pull up the ss you are attempting to view")
Resume Next

and the error occured and the msgbox appeared but froze the app.

CatDaddy
06-29-2012, 09:53 AM
ErrHandler:
MsgBox(..)
Err.Clear
Resume Next

Codeblue
06-29-2012, 10:00 AM
this had the same result

ErrHandler:
MsgBox ("You must log into EMaint and pull up the ss you are attempting to view.")
Err.Clear
Resume Next

I press the ok button on the msgbox and it just stays there frozen.
I tried this in a few different places

On Error Goto ErrHandler:

CodeNinja
06-29-2012, 10:20 AM
You may want to have different error handlers for different errors... You can always check err.Number to see what kind of error it is, then with select case or however you wish, you can give the appropriate message or actually handle the error.

CatDaddy
06-29-2012, 10:38 AM
and it should be
On Error Goto ErrHandler
no colon

Codeblue
06-29-2012, 10:46 AM
I tried this

ErrHandler:
MsgBox _
"An unexpected error has been detected" & Chr(13) & _
"Description is: " & Err.Number & ", " & Err.Description

I press ok and it goes away but another error msgbox comes up that won't go away.

"An unsuspected error has been detected, description is 9, subscript is out of range"

Actually if I press ok about 20x it goes away.

CatDaddy
06-29-2012, 10:59 AM
is this part of a loop that has multiple errors in a row?

CatDaddy
06-29-2012, 12:29 PM
did not answer my question...can you post your complete code?

Codeblue
06-29-2012, 03:21 PM
Some of this I cannot show for security reason...

Private Sub cmdViewAssetHistory_Click()

On Error GoTo ErrHandler
With ActiveWindow
.Width = 1359
.Height = 600
.DisplayGridlines = True
End With

Columns("B:B").Select ' center justify column B
With Selection
.HorizontalAlignment = xlCenter
End With

Range("A1:G1").Font.Bold = True ' rename column headers
Range("A1") = "Asset ID"
Range("B1") = "W/O"
Range("C1") = "Tech"
Range("D1") = "W/O Date"
Range("E1") = "W/O Status"
Range("F1") = "Type"
Range("G1") = "Comments"
'
With ActiveWindow
.SplitColumn = 0
.SplitRow = 1
End With

Range("A1").Select
ActiveWindow.FreezePanes = True ' freeze pane
Columns("A:A").ColumnWidth = 16.57 ' set column widths
Columns("B:B").ColumnWidth = 10.1
Columns("C:C").ColumnWidth = 18.1
Columns("D:D").ColumnWidth = 20.1
Columns("E:E").ColumnWidth = 14.1
Columns("F:F").ColumnWidth = 12.1
Columns("G:G").ColumnWidth = 120.1
Exit Sub
ErrHandler:
MsgBox _
"An unexpected error has been detected" & Chr(13) & _
"Description is: " & Err.Number & ", " & Err.Description '" & Chr(13) & " '_
' "Module is: commandbutton_click" & Chr(13) & _
' "Please note the above details before contacting support"
Resume Next
' MsgBox ("You must log into EMaint and pull up the ss you are attempting to view.")
' Err.Clear
' Resume Next
End Sub

Paul_Hossler
06-30-2012, 06:01 AM
I'm wondering why you have Resume Next in the error handler

That will just continue with the line after the one that caused the error, and you haven't really addressed the error condidtion

Resume (w/o the 'Next') will try the offending statement again, presumably after you've address the error causing condition

Possibly what you're looking to do is like


Option Explicit
Sub test()
Dim x As Long
Dim vbAnswer As VbMsgBoxResult


On Error GoTo ErrHandler

x = 1 / 0


Exit Sub
ErrHandler:

vbAnswer = MsgBox("You must log in to eMaint. Do that now and then hit [OK] or [Cancel] to forget it", _
vbCritical + vbOKCancel, "My Prog")

If vbAnswer = vbCancel Then
Exit Sub
Else
Resume 'NOT Resume Next
End If
End Sub


Paul