How to operate error in a sub? (VBA)

  • Thread starter Martin \(Martin Lee\)
  • Start date
M

Martin \(Martin Lee\)

In a module, I write a SUB() , for example:


Sub aipr()
.........

End Sub


But, sometimes this aipr() running would occur ERROR. So, I want to put an
ERROR OPERATION sentence inside this sub, How should the VBA be?

Thanks!


Martin Lee
 
D

Dirk Goldgar

Martin (Martin Lee) said:
In a module, I write a SUB() , for example:


Sub aipr()
........

End Sub


But, sometimes this aipr() running would occur ERROR. So, I want to
put an ERROR OPERATION sentence inside this sub, How should the VBA
be?

For example:

'----- start of example code -----
Sub aipr()

On Error GoTo Err_Handler

' ... main body of code ...

Exit_Point:
Exit Sub

Err_Handler:
' Special code to handle error number 2501
If Err.Number = 2501 Then
' We'll ignore this one and resume at next statement
Resume Next
Else
' Display a message and exit.
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_Point
End If

End Sub
'----- end of example code -----

In the VB Editor, search the help for "error handling".
 
M

Martin \(Martin Lee\)

Thank you very much!

Martin Lee


Dirk Goldgar said:
For example:

'----- start of example code -----
Sub aipr()

On Error GoTo Err_Handler

' ... main body of code ...

Exit_Point:
Exit Sub

Err_Handler:
' Special code to handle error number 2501
If Err.Number = 2501 Then
' We'll ignore this one and resume at next statement
Resume Next
Else
' Display a message and exit.
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_Point
End If

End Sub
'----- end of example code -----

In the VB Editor, search the help for "error handling".

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top