Error Handling

  • Thread starter Thread starter Tony Toews
  • Start date Start date
T

Tony Toews

Ant said:
I have used the code below witch works if there is an error but also shows
the message box when there is no error but code runs anyway after you OK the
message box.



I can’t see what it is I am doing wrong, can any one help?



Private Sub ComFilter_Click()



On error GoTo EntDate

‘My Good code goes hear
exit sub

EntDate:

MsgBox “My message txt”

End Sub

Insert the above exit sub line as you see.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
 
While the others have told you hold to avoid receiving unnecessary error
messages, they did miss out what I consider to be an important step.

When you do have an error, and you've reported it to your users, you really
should reset the error object so that Access doesn't think there's still an
error. Using the Resume command will do this, as will using the Clear method
of the Err object.

My recommendation is:

Private Sub ComFilter_Click()
On error GoTo Err_ComFilter_Click

‘ Good code goes here

End_ComFilter_Click:
Exit Sub

Err_ComFilter_Click:
MsgBox “My message txt”
Resume End_ComFilter_Click

End Sub

In this way, if you have anything that you want to happen whether or not
there's an error, you can put those statements between End_ComFilter_Click
and Exit Sub.
 
I am new to VB and having a bit of difficulty with error handling, I have
done some code witch works fine providing the user fills in all the fields
however if they don’t I get an error and the debug window. As this would
freak out my average user I need to include some error handling in the way
of a message box to explain the error of there ways.



I have used the code below witch works if there is an error but also shows
the message box when there is no error but code runs anyway after you OK the
message box.



I can’t see what it is I am doing wrong, can any one help?



Private Sub ComFilter_Click()



On error GoTo EntDate

‘My Good code goes hear



EntDate:

MsgBox “My message txt”

End Sub







Thank
 
I agree with Douglas.

I typically use the following code as it is easier to copy and paste from one procedure to the next.

Private Sub ComFilter_Click()
On Error GoTo ErrorHandler

‘My Good code goes hear

ExitProc:
Exit Sub

ErrorHandler:
MsgBox “My message txtâ€
Resume ExitProc
End Sub

By using the 'ErrorHandler' and 'ExitProc' labels in every procedure, you'll save yourself a lot of typing ( as compared to 'Err_ComFilter_Click' and 'End_ComFilter_Click')

If you want to take an even more proactive approach, you could use a validation function prior to processing user input

Private Sub ComFilter_Click()
On Error GoTo ErrorHandler

If Validate_UserInput() = False then goto ExitProc

‘My Good code goes hear

ExitProc:
Exit Sub

ErrorHandler:
MsgBox “My message txtâ€
Resume ExitProc
End Sub

The Validate_UserInput() procedure would check that all required user input is provided and is within bounds and of the right data type.

Have fun,

itdoghouse
 
Actually, if you download MZTools from www.mztools.com you can add the error
handling without any typing. <g>

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)



itdoghouse said:
I agree with Douglas.

I typically use the following code as it is easier to copy and paste from one procedure to the next.

Private Sub ComFilter_Click()
On Error GoTo ErrorHandler

'My Good code goes hear

ExitProc:
Exit Sub

ErrorHandler:
MsgBox "My message txt"
Resume ExitProc
End Sub

By using the 'ErrorHandler' and 'ExitProc' labels in every procedure,
you'll save yourself a lot of typing ( as compared to 'Err_ComFilter_Click'
and 'End_ComFilter_Click')
If you want to take an even more proactive approach, you could use a
validation function prior to processing user input
Private Sub ComFilter_Click()
On Error GoTo ErrorHandler

If Validate_UserInput() = False then goto ExitProc

'My Good code goes hear

ExitProc:
Exit Sub

ErrorHandler:
MsgBox "My message txt"
Resume ExitProc
End Sub

The Validate_UserInput() procedure would check that all required user
input is provided and is within bounds and of the right data type.
 
Back
Top