[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 151
  • Last Modified:

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

0
broadbent
Asked:
broadbent
  • 7
  • 4
  • 2
  • +1
1 Solution
 
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 7
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now