Link to home
Start Free TrialLog in
Avatar of broadbent
broadbentFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of ture
ture

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of caraf_g
caraf_g

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?
Avatar of broadbent

ASKER

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
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"
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
FoundName = Coll.Item(MyName).Name

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


 
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.
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.
I've tried your code, but it still fails. It doesn't fail elsewhere though.

I am using VB6 Version 8176
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"
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.
Can you post the rewritten code here?
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.
Exellent observation AnswerTheMan.
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