Procedure - wide error handling

O

Outatym

I am needing to create error handling for my program. I have one main
procedure that runs on a start button. I don't forsee any errors occurring
with the project, but with my type of users, that will most likely be my
downfall. I have decided that error handling will be needed, but the code
will not compile. Keeps giving "Label not Defined" errors. Is there a way
to catch any error witn the procedure, create an email to me which the users
will tell what error code they encountered and where, then terminate the
program? Below is my current code...

Private Sub CmdRun_Click()

On Error GoTo 1227

'
'
' lots of code
'
'

Dialogs(wdDialogFilePrint).Show

PPSPrintForm.Show

ActiveDocument.PrintOut (Background = False)

If Err.Number <> 0 Then
MsgBox "An error has occurred, the program will now terminate and
create an troubleshooting email. The error message number is:" & Error(Err)
DoCmd.SendObject acSendNoObject, , , "(e-mail address removed)"
Err.Clear
Application.Quit
End If

End Sub
 
J

Jean-Guy Marcil

Outatym said:
I am needing to create error handling for my program. I have one main
procedure that runs on a start button. I don't forsee any errors occurring
with the project, but with my type of users, that will most likely be my
downfall. I have decided that error handling will be needed, but the code

If your code is well written and thoroughly tested, there should not be any
unforseen errors...
I alway try to foresee the errors users will make and code accordingly. For
instance, if my code works with tables, but the the cursor is not in a table,
I catch it, sends a message to the user and abort. If I need an insertion
point, I collpase the selection. Etc.

I also test my code with many types of selection or environment...

But if your code is very complex and you do not have the time to break it
down into a bunch of smaller routines that can be thoroughlky tested (clients
are not always willing to pay for sturdy code...) then, you do need error
trapping.
will not compile. Keeps giving "Label not Defined" errors. Is there a way
to catch any error witn the procedure, create an email to me which the users
will tell what error code they encountered and where, then terminate the
program? Below is my current code...

Private Sub CmdRun_Click()

On Error GoTo 1227

'
'
' lots of code
'
'

Dialogs(wdDialogFilePrint).Show

PPSPrintForm.Show

ActiveDocument.PrintOut (Background = False)

If Err.Number <> 0 Then
MsgBox "An error has occurred, the program will now terminate and
create an troubleshooting email. The error message number is:" & Error(Err)
DoCmd.SendObject acSendNoObject, , , "(e-mail address removed)"
Err.Clear
Application.Quit
End If

End Sub

In VBA

On Error GoTo 1227

must refer to a label within the procedure. Labels are simply created by
typing a string followed by a ":"

So, you would need:


Private Sub CmdRun_Click()

On Error GoTo ErrHandle

'
'
' lots of code
'
'

Dialogs(wdDialogFilePrint).Show

PPSPrintForm.Show

ActiveDocument.PrintOut (Background = False)

Exit Sub 'This is needed so that the error handling code is skipped if no
error occurs.

ErrHandle:
If Err.Number <> 0 Then
MsgBox "An error has occurred, the program will now terminate and
create an troubleshooting email. The error message number is:" & Err.Number
DoCmd.SendObject acSendNoObject, , , "(e-mail address removed)"
Err.Clear
Application.Quit
End If

End Sub
 
O

Outatym

Awesome!!! Thank you for your help!

This is why I love coming here...always can get a answer that I can
understand.
 

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