select case for error

please give me a code that each time there a error it will go directly to select cased statement, its mean that i dont need to declare 1 by 1 the error for each job.
JOHNHOAsked:
Who is Participating?
 
hessConnect With a Mentor Commented:
on error goto errorhandeler
0
 
hessCommented:
on error goto error handeler


(code)
(code)
(code)

exit sub
errorhandeler:
(select case.....)

is that what you need?
0
 
tureCommented:
JOHNHO,

Create a form with only one commandbutton, enter this code:

Private Sub Command1_Click()
  Dim x As Byte
 
  'All errors will branch to errhandler
  On Error GoTo errhandler
 
  'Enter a value for x
  x = InputBox("Enter a value for x:")
 
  'If x is 10 - 19 cause an overflow error
  If x >= 10 And x < 20 Then x = x * 1000
 
  'If x is 20 - 29, cause a division by 0 error
  If x >= 20 And x < 30 Then x = x / 0
 
  'If x >= 30, cause a file not found error
  If x >= 30 Then
    Open "c:\test\nonexist.txt" For Input As 1
    Close 1
  End If
 
  'Turn off error handling.
  On Error GoTo 0
 
  'Get out of this procedure before reaching the error handler
  Exit Sub
 
errhandler:
 
  'Display the error number and error description
  MsgBox Err.Number & ": " & Err.Description
 
  'Perform different tasks depending on the error
  Select Case Err.Number
    Case 11 'Division by 0
      x = 0
      'Continue execution after the statement that caused the error
      Resume Next
   
    Case 53 ' File not found
      MsgBox "You need to create the file first."
      'Continue execution after the statement that caused the error
      Resume Next
     
    Case Else
      MsgBox "Unknown error. Cancelling operation abruptly."
      End
     
  End Select
End Sub

Ture Magnusson
Karlstad, Sweden
0
 
Éric MoreauSenior .Net ConsultantCommented:
Do you want that any error that occurs in any subs go to one error handler without placing error handling in any of these subs? If this is what you want (what we called a general on error goto ...), THIS CANNOT BE DONE IN VB!

You have to write code in each sub in which you want error trapping!
0
 
FeresCommented:
if i understood well, and emoareau is saying what you need to know, then there is a way.
create a module and place a sub named Main then go to project properties and set start up object for sub Main so:

on error goto errorHandle
frmMain.Show
exit sub
errorHandle:
'Place select case here
select case err.Number
  case 123
  case 321
  .
  .
  .
end select

if you want to disable the handle error and let vb raise an error set

on error goto 0

but if you want to handle a particular error in a sub

on local error goto errorHandle

exit sub
errorHandle:

end sub

Well, that's the idea.
0
All Courses

From novice to tech pro — start learning today.