Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error handler not handling errors

Status
Not open for further replies.

olichap

Programmer
Mar 20, 2001
389
US
Hi all,

I'm have a bit of trouble getting an error handler to work properly. In my code I am calling a function based to which I pass a string variable (think of it as a CustID fields).

When the function can not find any data that relates to the string I get an "Item is missing" error; my error handler drops the code down to the end of the function, which then resets a couple of other variables and loops back up to the original function. The idea is to call the function a slightly different way the second time through (like querying a different table).

The problem is that if/when the function STILL can not find the data and the error is returned VB does NOT drop down the the error handler a second time (where I have code that should deal with this eventuality).

I am unclear as to why the code does not capture the error a second time. I even tried putting the On Error GoTo right after the label to which I loop back up; still only works once.

Any ideas?

Oliver
 
It is happening becaust you program is still in an error state. You need to come out of the procedur to the calling procedure then go back in, perhaps using a parameter to control the version of the search that you pass in.

Hope this helps,

Dan
 
Basically, VB doesn't support nested error handling. Once an error is raised you are dropped into the error handler and cannot raise another error until the error handler is properly exited. And the only real ways to exit a running error handler are Exit/End Sub/Function or Resume/Resume next.

I suspect from your description that you are using some form of GoTo to loop back into the function. Which means that, in fact, you are still in the error handler...

 
The problem is that once an error trap has been tripped, it's not reset without a resume or resume next, although the on error goto label should be working...

Anyway, the real problem here is that the error is being generated within the function that you are calling, and so that's where error trapping should be. If an error occurs, you can return a value for the function (say -1 if numeral) that lets you know an error occurred. Then you would know to change some parameters and try the function again:

'attempt function call
nResult = Function(parameters...)

if nResult = -1
'error in function, change parameters and try again
parameters = something different

nResult = Function(parameters...)
end if

If you want to keep trying until there are no errors then change the if-then to a loop:

do while nResult = -1
'error in function, change parameters and try again
parameters = something different

nResult = Function(parameters...)
loop

Hope this helps -Chris Didion
Matrix Automation, MCP
 
Thanks Dan et al,

It took a little re-structuring but your answers helped greatly.

Oliver
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top