Why doesn't my onError work? (Resolved)
I'm writing a word plugin which connects to a back end database. I've got the following code:-
Code:
On Error GoTo connErr61
mConn.Open "Provider=SLXNetwork.1;User ID =" & mUserID & ";password=" & mPassword & ";Data Source = " & mDB & ";Extended Properties=""SLX Server=" & mServer & ";ADDRESS=localhost;Type=ODBC;PORT=" & mPort & """"
MsgBox "open succeeded"
mConn.CursorLocation = adUseClient
Exit Sub
connErr61:
MsgBox "open failed"
If MsgBox("Database connection failed. Check configuration? (Cancel will exit)", vbOKCancel, "Connection Error") = vbOK Then
configure
Else
Err.Raise 100, "Config.Connect", "Connection Failed"
End If
End Sub
The idea is that if the connection fails I give the user the option to configure the connection (via the configure method) or not to bother. If the user doesn't bother then the app should raise an error which will be caught at the top level and exit gracefully.
The trouble is that if I give it an invalid password I get a runtime error on the mConn.open line and the app bombs out immediately. Neither the "open succeeded" or "open failed" message appears and, more importantly, the code which lets the user reconfigure doesn't fire. As a result, once there's a bad password the user can't get into the app at all.
Shouldn't my "onError GoTo connErr61" force execution in to the connErr61 block?
Re: Why doesn't my onError work?
Have you set the error trapping options in VB? Tools->options->General->error trap
It could also be a good idea to use " On error resume next" allowing the call to complete, and then check the return value (if there is one) to if the call has succeded.
Re: Why doesn't my onError work?
Can you post the complete sub, please.
Re: Why doesn't my onError work?
Quote:
Originally Posted by HFMa
Have you set the error trapping options in VB? Tools->options->General->error trap
It could also be a good idea to use " On error resume next" allowing the call to complete, and then check the return value (if there is one) to if the call has succeded.
If I hear one more person suggest "on error resume next" I'm going to beat them to a bloody pulp. That's the WORST thing you can do... EORN is not error handling but error ignoring.
Funky - two things: 1) Right-click in your code, select the "Toggle" menu... what is your error setting? 1) Break on all errors; 2) Break in Class; 3) Break only on Unhandled Errors.
Make sure it is set to #3 option.
Second, what does happen? - nevermind, I see it now.... OK, then is this happening during design (ie, when you've gotthe code loaded & running the app) or only when running the compiled app (I've seen cases where it worked in one, and not the other.)
Tg
Re: Why doesn't my onError work?
Quote:
Originally Posted by techgnome
If I hear one more person suggest "on error resume next" I'm going to beat them to a bloody pulp. That's the WORST thing you can do... EORN is not error handling but error ignoring.
Funky - two things: 1) Right-click in your code, select the "Toggle" menu... what is your error setting? 1) Break on all errors; 2) Break in Class; 3) Break only on Unhandled Errors.
Make sure it is set to #3 option.
Second, what does happen? - nevermind, I see it now.... OK, then is this happening during design (ie, when you've gotthe code loaded & running the app) or only when running the compiled app (I've seen cases where it worked in one, and not the other.)
Tg
I agree. It'd be nice to see the complete function, though.
Re: Why doesn't my onError work? <resolved>
Hi Guys
I hadn't had any answers on this after the initial silence and I've since got around the problem by making the user define which connection string to use as part of configuration. So I can't post the complete function because it's been rewriten and no longer exhibits the problem. Thanks for the offer though.
I basically always use 'Break on unhandled' because. Occasionally I'll switch it to break on all as this can help find errors that my error handlers are 'hiding' but 'unhandled' is as close as possible to real world so provides the best test.
Re: Why doesn't my onError work?
Mark this thread as resolved?