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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

On Error problem

Status
Not open for further replies.

accjrf

Technical User
Apr 1, 2004
39
US
I have some code that i am trying to loop through. Dont pay attention to the counter or any of that, I just need help with why the error is handled ok the first time through, but on the second time it wont go to A:?? On the second loop through the error box pops and halts the code, why doesnt the goto A: work for the second loop through? The AppActivate statement errors because the PRINT box can take several seconds to display in IE before i can move on.

do until counter = 100

on error goto A:

AppActivate("PRINT")

A: (wait for 2 seconds)
counter = counter +1

loop
 
In the VBA help have a look to the Resume instruction.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Which statement is indicated by the error box?

_________________
Bob Rashkin
 
not sure what you are asking me.

If fails on the "AppActivate ("PRINT")" line the second time through.

I thought as long as the loop would send the code through the "On Error GoTo A:" every time, that it would handle the error the same every time?
 
Seems you don't know how to use error handling.
Did you follow my previous suggestion ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
1. please do as PHV suggests and look up Resume.

2. please do some research on error-handling, as PHV also suggests.

For example, you may want to think about WHERE you have the On Error line, and WHERE you have A:

" Dont pay attention to the counter or any of that"

Au contraire, I suggest you DO pay attention to the counter "or any of that".

Gerry
 
Personally I think telling you to look in Help isn't the most helpful suggestion in this case. The way that error handling works in VBA is clunky and not really intuitive, and the explanations generally provided tend to make sense to people who already understand the process and be as clear as mud to everybody else.

Firstly you should consider each Procedure (Sub or Function) as a separate unit. Within an individual procedure, error handling is either "enabled" or it isn't.

If it is *not* enabled then, when an error occurs, the error is considered to have happened in the higher level procedure which called the one in which the error actually occurred - and the same determination of whether or not error handling is enabled in that higher level procedure takes place. Error handling is always, effectively, enabled at the very highest level (VBA itself, which calls your top level procedure) and, if there is nothing else then that will kick in and give you the standard VBA error msgbox.

If it *is* enabled, then, when an error occurs, the error handling procedure will be invoked, at which point it is considered "active" and, in effect, no longer "enabled". You remain in error handling mode, with your error handler "active" until you do something about it. That something is to Resume normal code. Most of the time, your code can run either in error handling mode or not in it, and there are very few differences you can observe.

So, in your example, the first time you get an error, your enabled error handler becomes active and, as you don't Resume, the next time you get an error you do not have an enabled error handler so the error is considered to be in the calling routine (VBA itself), which handles the error by issuing the msgbox you see.

Error handling remains enabled until either it becomes active, or it is explicitly disabled. When you resume, your active error handler becomes enabled again; you do not need to constantly enable it and having your On Error inside a loop is pointless unless it is also explicitly disabled within the same loop.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Tony,

Thanks. I appreciate the explanation. I figured out on Friday that all I was missing was the RESUME line. (Although VBA help wasnt much of one, I found it in a book)Once I added that it worked like a charm.

PHV was right that I didnt know how to use error handling. That is why I asked for help but felt like all I got was a condescending remark "Au contraire" and the suggestion that I had things in the wrong place...which I didnt.

I do appreciate the help because every day is a learning experiance for me. Many Thanks!!!
 
Tony}having your On Error inside a loop is pointless [/quote said:
That is what I was hinting at when I mentioned about WHERE the On Error is.

"and the suggestion that I had things in the wrong place...which I didnt. "

Oh, but you do. You do have it in the wrong place. And for precisely the reason Tony mentions.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top