on error

I have a subroutine that takes a list of names and sees whether or not each one is included in a collection

Being lazy I thought I would do this:-

while ...
 on error resume next
 FoundName = Coll.Item(MyName)
 If FoundName="" then ...
loop

The on error is in the loop, but it works once and then fails and I get the VB error.

Ideas

LVL 4
broadbentAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tureCommented:
broadbent,

Try this...

Do While ...
 On Error Resume Next
 FoundName = ""
 FoundName = Coll.Item(MyName).Name
 If FoundName = "" Then ...
 On Error Goto 0
Loop

Ture Magnusson
Karlstad, Sweden
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
caraf_gCommented:
ture's answer is definitely an improvement on your code, broadbent, but it doesn't seem to be addressing your problem with the error handling.

From what you've given us there is no reason why the code should throw an error at all.

Are you using any class modules?
0
broadbentAuthor Commented:
Thanks caraf_g for that comment.
I am not using any class modules.
and yes the actual code is more like the improvement, but when you're on-line you try to get to the point.

However it is as simple as

on error resume next
code with possible error
continue
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

caraf_gCommented:
Well, I tried the following:

Dim lngCounter As Long
While lngCounter < 100
    On Error Resume Next
    MsgBox 1 / 0
   
    lngCounter = lngCounter + 1
Wend


and it ran fine...
The only way I could get it to break on the above was when I went into Tools-Options-"General" tab and checked "Break on all errors"
0
tureCommented:
broadbent,

Did my code help?

I believe that the key is to set the value of FoundName to an empty string before trying to retrieve it from your collection. If you don't do that, FoundName will still contain the value of the last succesful access to the collection and your If FoundName = "" will not perform as expected.

Also... perhaps it is not the .Name property of the item that you want to retrieve, if so - change .Name in my code to wthe property you want.

Do While ...
 On Error Resume Next
 FoundName = "" 
 FoundName = Coll.Item(MyName).Name
 If FoundName = "" Then ...
 On Error Goto 0
Loop

/Ture
0
AnswerTheManCommented:
FoundName = Coll.Item(MyName).Name

try to check what FoundName value is, when reffering to NON-EXISTING item.


 
0
caraf_gCommented:
Hello Broadbent,

I tried AnswerTheMan's suggestion, in the following code:

Option Explicit

Private Sub Form_Load()

Dim Coll As Collection

Set Coll = New Collection

Dim MyName As String

Dim FoundName As String

Do While True
 On Error Resume Next
 FoundName = ""
 FoundName = Coll.Item(MyName).Name
 If FoundName = "" Then
 End If
 On Error GoTo 0
Loop

End Sub

....but I still can't reproduce the problem, even though the collection is definitely empty and every reference to Coll.Item(MyName).Name must therefore fail.

The "On Error Resume Next" statement takes care of the error in every iteration, not just the first one.

Also tried variations such as not setting Coll to a new collection, but none of it could reproduce the problem.
0
caraf_gCommented:
Sorry, sorry, sorry!!

This is the third time I've posted a comment, and I've been meaning to ask you this since the thread started...

What version of VB are you using? Any service packs? It's a long shot... but I have to try every angle.
0
broadbentAuthor Commented:
I've tried your code, but it still fails. It doesn't fail elsewhere though.

I am using VB6 Version 8176
0
caraf_gCommented:
That's really strange... So you're saying it still works like you said before? It goes into the loop, it works the first iteration and it throws an error on the next one?

I just checked my VB; it's version 8464, with service pack 3.

If you're still having the problem as I just described, the only thing I can think of is that it's a problem with your version of VB. I just cannot think of another explanation. If however the error happens immediately (on the very first iteration) check your Tools-Options-"General" tab and see if you checked "Break on all errors"
0
broadbentAuthor Commented:
The error is trapped the first time , but not on the reiteration.

As it happens I've rewritten the code to loop though each member of the collection.
0
caraf_gCommented:
Can you post the rewritten code here?
0
AnswerTheManCommented:
i wonder what is written in place of the folowing <WORD> :
 do while <WORD>
 ...
 ...
 loop

if <WORD> is some condition reffering to somthing that due to the FIRST error is now NULL - then you'll have an error right on the SECOND time the loop goes.
0
caraf_gCommented:
Exellent observation AnswerTheMan.
0
broadbentAuthor Commented:
Sorry everybody for not getting back, but I branched off to other things while this got sorted. I'll start looking at your suggestions and get back.
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.