On Error Handling

Topher

Registered User.
Local time
Today, 19:08
Joined
Apr 18, 2000
Messages
72
Would anyone know why the code for error handling does not work??

ie:

Private Sub Command1()
On Error GoTo Err_Command1

DO CODE

Exit_Command1:
End Sub

Err_Command1:
Msgbox err.description
Resume Exit_Command1

End Sub

I've used this code before and it worked fine but now it will not work in another program i am creating....

any ideas??

- Topher
 
Hi there.

On your OnError statement you are missing a colon at the end. It should read

On Error Goto Err_Command1:

This works for me but try it and see. If this doesn't work and you are still getting error messages then post the error message here and I'll try and help
 
That Seems to have worked!
Thanks!

funny thing is though, in other programs i've never used the colon on the On Error portion and it works fine......

- Topher
 
I am intrigued by this one when you say it works now because it should not.
One problem I can see with the code is that you have an End Sub directly after the Exit_Command1:, this should be an 'Exit Sub 'not and 'End Sub'
So your code should read

Private Sub Command1()
On Error GoTo Err_Command1

DO CODE

Exit_Command1:
Exit Sub 'Not end sub

Err_Command1: ' This is the label
Msgbox err.description
Resume Exit_Command1

End Sub

If you left it as as 'end sub' you would get an error stating
'Compile error Label not defined'
this is because the error label, and code following it' is effectivly outside the procedure. The 'End sub' terminates the sub.
To test this you can use the following

Private Sub Command1()
Dim a As Integer
On Error GoTo Err_Command1

Err.Raise 6

Exit_Command1:
Exit Sub

Err_Command1:
MsgBox Err.Description
Resume Exit_Command1

End Sub

Where 'err.raise 6' forces an overflow error condition.
This should work
If you know change the exit sub to end sub and try to either compile the module or run it you will get the 'label not defined message'
This applies to 97 I am not sure about 2000.
Note to other users who may not know about the err.raise this can be very useful for testing err handling in procedures simply assign the erro number you want to test to the err.raise.
Hope this helps
Regards
Trevor from www.accesswatch.co.uk
 
Yep your right it wasn't workin b/c of the end sub. but it still wasnt workin even when i put in exit sub. after i put the colon in it worked fine.....

Thanks for the explanation though

- Topher
 

Users who are viewing this thread

Back
Top Bottom