473,320 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

On error resume next bug

Neo
I found on error resume next doesn't work in for each... e.g.

on error resume next
for each x in y
'do stuff
next

if you have an error in for each loop, it falls in infinite loop... it
doesn't move to next element... this sad thing but, it's indication
that we must move to try catch instead of on error.
-Neo

Jul 17 '06 #1
4 3736
It works for me.

'VB 2005
Sub Main()
On Error Resume Next
Dim c As New Collection
c.Add(1)
c.Add(2)
c.Add("A")
c.Add(3)
c.Add("B")
Dim i As Integer
Dim v As Object
i = 0
For Each v In c
i = i + v
Debug.Print(i)
Next
Debug.Print(i)
End Sub

I wrote and tested this code in both VB 6 and 2005. The real issue here is
the use of the On Error Resume Next statement. It should be replaced
whereever feasible with try catch statements.

Mike.

"Neo" <pr*********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
I found on error resume next doesn't work in for each... e.g.

on error resume next
for each x in y
'do stuff
next

if you have an error in for each loop, it falls in infinite loop... it
doesn't move to next element... this sad thing but, it's indication
that we must move to try catch instead of on error.
-Neo

Jul 17 '06 #2
Neo
But does it really raise error? If yes, after error does it move to
next element?

My error was during traversing graphs collection in excel worksheet.The
problem I had when there was an error which made the infinte loop. I am
not sure if that was only to do with that particualar access, bottom
line is say bye, bye to "on error resume next"

Michael D. Ober wrote:
It works for me.

'VB 2005
Sub Main()
On Error Resume Next
Dim c As New Collection
c.Add(1)
c.Add(2)
c.Add("A")
c.Add(3)
c.Add("B")
Dim i As Integer
Dim v As Object
i = 0
For Each v In c
i = i + v
Debug.Print(i)
Next
Debug.Print(i)
End Sub

I wrote and tested this code in both VB 6 and 2005. The real issue here is
the use of the On Error Resume Next statement. It should be replaced
whereever feasible with try catch statements.

Mike.

"Neo" <pr*********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
I found on error resume next doesn't work in for each... e.g.

on error resume next
for each x in y
'do stuff
next

if you have an error in for each loop, it falls in infinite loop... it
doesn't move to next element... this sad thing but, it's indication
that we must move to try catch instead of on error.
-Neo
Jul 17 '06 #3
Yes, it raises a "First Chance Exception" and then loops to the next item.
I don't know if the Excel Graphs collection is a true COM collection. You
may be better off using

For i as Integer = 0 to Graphs.Count -1
dim gr as Excel.Graph = Graphs(i)
' Do something with gr
next i

Having worked with Excel, I have discovered that some of Excel's collections
are true COM collections and others require using the count method above.

You are correct in that you really need to dump On Error Resume Next. You
should also add "Option Explicit On" and "Option Strict On" at the top of
all your code modules. This will allow the VB compiler to find and flag all
sorts of variable typing errors. You'll be surprised at how many common VB
6 constructs are simply dangerous - Option Strict On will catch 99+% of
them.

Mike.
"Neo" <pr*********@gmail.comwrote in message
news:11**********************@35g2000cwc.googlegro ups.com...
But does it really raise error? If yes, after error does it move to
next element?

My error was during traversing graphs collection in excel worksheet.The
problem I had when there was an error which made the infinte loop. I am
not sure if that was only to do with that particualar access, bottom
line is say bye, bye to "on error resume next"

Michael D. Ober wrote:
It works for me.

'VB 2005
Sub Main()
On Error Resume Next
Dim c As New Collection
c.Add(1)
c.Add(2)
c.Add("A")
c.Add(3)
c.Add("B")
Dim i As Integer
Dim v As Object
i = 0
For Each v In c
i = i + v
Debug.Print(i)
Next
Debug.Print(i)
End Sub

I wrote and tested this code in both VB 6 and 2005. The real issue here
is
the use of the On Error Resume Next statement. It should be replaced
whereever feasible with try catch statements.

Mike.

"Neo" <pr*********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
I found on error resume next doesn't work in for each... e.g.
>
on error resume next
for each x in y
'do stuff
next
>
if you have an error in for each loop, it falls in infinite loop... it
doesn't move to next element... this sad thing but, it's indication
that we must move to try catch instead of on error.
-Neo
>


Jul 18 '06 #4
Neo
Yes, I fully agree with you. I was still usin "on error resume next"
since it was easier. but if it doesnt' work even in one case, shouldn't
be used. Also, once you "on error" you can't use try catch.. that sux.
better not to use "on error".

about option strict and option explicit. I have been always using them.
Thanks for suggesting that, it might be useful to other VB 6 developer
who don't use it.
-Neo
Michael D. Ober wrote:
Yes, it raises a "First Chance Exception" and then loops to the next item.
I don't know if the Excel Graphs collection is a true COM collection. You
may be better off using

For i as Integer = 0 to Graphs.Count -1
dim gr as Excel.Graph = Graphs(i)
' Do something with gr
next i

Having worked with Excel, I have discovered that some of Excel's collections
are true COM collections and others require using the count method above.

You are correct in that you really need to dump On Error Resume Next. You
should also add "Option Explicit On" and "Option Strict On" at the top of
all your code modules. This will allow the VB compiler to find and flag all
sorts of variable typing errors. You'll be surprised at how many common VB
6 constructs are simply dangerous - Option Strict On will catch 99+% of
them.

Mike.
"Neo" <pr*********@gmail.comwrote in message
news:11**********************@35g2000cwc.googlegro ups.com...
But does it really raise error? If yes, after error does it move to
next element?

My error was during traversing graphs collection in excel worksheet.The
problem I had when there was an error which made the infinte loop. I am
not sure if that was only to do with that particualar access, bottom
line is say bye, bye to "on error resume next"

Michael D. Ober wrote:
It works for me.
>
'VB 2005
Sub Main()
On Error Resume Next
Dim c As New Collection
c.Add(1)
c.Add(2)
c.Add("A")
c.Add(3)
c.Add("B")
Dim i As Integer
Dim v As Object
i = 0
For Each v In c
i = i + v
Debug.Print(i)
Next
Debug.Print(i)
End Sub
>
I wrote and tested this code in both VB 6 and 2005. The real issue here
is
the use of the On Error Resume Next statement. It should be replaced
whereever feasible with try catch statements.
>
Mike.
>
"Neo" <pr*********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
I found on error resume next doesn't work in for each... e.g.

on error resume next
for each x in y
'do stuff
next

if you have an error in for each loop, it falls in infinite loop... it
doesn't move to next element... this sad thing but, it's indication
that we must move to try catch instead of on error.
-Neo
Aug 4 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: jason | last post by:
Is there a way to avoid On Error Resume Next for: cnn.Open strCon SQL = "EXEC Customer @txtEmail='" & email_address & "'" set rs = cnn.execute(SQL) 'On error resume next rs("email_address")...
3
by: Michael | last post by:
Hello again everyone, Hope you can help. I have a form with a lot of code going on in the background but I can't find the route cause of the following error message. Update or Cancel Update...
13
by: deko | last post by:
I use this convention frequently: Exit_Here: Exit Sub HandleErr: Select Case Err.Number Case 3163 Resume Next Case 3376 Resume Next
2
by: deko | last post by:
I want to turn error handling off, then back on in a sub routine that backs up a select set of documents. For some reason, I can't seem to get this to work - Private Sub BackupDocs() On Error...
6
by: Squirrel | last post by:
I have a command button on a subform to delete a record. The only statement in the subroutine is: DoCmd.RunCommand acCmdDeleteRecord The subform's recordsource is "select * from tblVisit order...
33
by: Anthony England | last post by:
I am considering general error handling routines and have written a sample function to look up an ID in a table. The function returns True if it can find the ID and create a recordset based on...
10
by: Anthony England | last post by:
(sorry for the likely repost, but it is still not showing on my news server and after that much typing, I don't want to lose it) I am considering general error handling routines and have...
3
by: bob.needler | last post by:
I know On Error Resume Next is generally considered lazy. But can someone tell me why the resume next in Exit_Handler does not seem to work? It generates the typical unhandled runtime error...
11
by: Maxwell2006 | last post by:
Hi, I know that this is not a good practice, but I wonder do we have "on error resume next" in C#? Thanks,
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.