Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ADO errors

Posted on 2000-05-04
10
Medium Priority
?
160 Views
Last Modified: 2013-11-23
First my code:
do while not rsadd.EOF
on error goto errh:
rs.addnew
rs!Name=rsadd!name
rs!Surname=rsadd!surname
rs.update

errh:
rsadd.movenext
loop

My problem is this:
the name & surname fields are a unique index on my SQL server table. If that get violated the routine must just go on to the next record.
The first time the error occurs the errorhandler gets invoked, but not again after that. The second time it is a fatal runtime error? Why?
0
Comment
Question by:Heinrichsa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 6

Expert Comment

by:setiawan
ID: 2776520
Hi Heinrichsa,

do while not rsadd.EOF
on error goto errh:
rs.addnew
rs!Name=rsadd!name
rs!Surname=rsadd!surname
rs.update
errh:
if not rsadd.EOF then rsadd.movenext
loop

but I prefer to find the name first, if not found then add it into the table, by using connection object rather than recordset object addnew method

hope this helps
  danny
0
 

Author Comment

by:Heinrichsa
ID: 2776544
I know that is the way with no problems, but my rs table is huge and find methods take very long. I'll try the connection execute method. I'll report soon.
0
 
LVL 2

Accepted Solution

by:
JetScootr earned 800 total points
ID: 2776565
The reason it only works once is that, technically, you're misusing "On Error".  On Errror is intended to handle programmatic errors, not to control the flow of the program.  When an error occurs, control is transferred from your main body of code to an error handler, which is expected to then perform your "Ship is sinking, to the lifeboats!" code.  As a result, from VB's viewpoint, after the first error occurs, you're executing an error handler fromthat point on.  Jumping to the error handler while handling an error would result in (probably) fatal recursion loop, when On Error is used as intended.  I'm assuming here that rs and rsadd are actually the same table, and rsadd is a snapshot.  If rsadd is NOT a snapshot, and they are the same table, your logic or mine will fill your harddrive.  Rsadd should be a snapshot so that new records added by rs are not seen by rsadd.  Anyway, the   better logic is:


While Not rsadd.eof
     rs.Findfirst "Name = '" & rsadd!name & "' and " & _
           "surname = '" & rsadd!surname & "'"
     if rs.NoMatch then
         rs.Addnew
         rs!Name  = rsadd!name
         rs!surname = rsadd!surname
         rs.update
     endif
     rsadd.movenext
wend
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Expert Comment

by:JetScootr
ID: 2776581
About performance:  If Name & surname are unique, and are indexed,  note this:  You're performing the "Find" function anyway, internally to SQL server, when you do the AddNew.  The unique index should make it very fast.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 2776636
Another method using the errorhandler would be to correctly resume processing. The reason it won't get called again is that as has been said you are still within the error handling routine.

do while not rsadd.EOF
 on error goto errh:
 rs.addnew
 rs!Name=rsadd!name
 rs!Surname=rsadd!surname
 rs.update
errResume:
 rsadd.movenext
loop
Exit Sub

errh:
  Resume errResume
End Sub

Would be more correct as it returns control to the original code but at a different point.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2776663
TimCottee, your code is almost perfect, but it misses the Cancel method (to undo the AddNew:

do while not rsadd.EOF
 on error goto errh:
 rs.addnew
 rs!Name=rsadd!name
 rs!Surname=rsadd!surname
 rs.update
errResume:
 rsadd.movenext
loop
Exit Sub

errh:
  rs.Cancel
  Resume errResume
End Sub
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 2776787
I meant to put that in but forgot, so thanks for the comment, though it should be rs.CancelUpdate rather than Cancel which is for connection or open methods.
0
 
LVL 2

Expert Comment

by:JetScootr
ID: 2776945
angelIII,TimCottee, the problem with using that approach is that it assumes the only error that will occur is a duplicate key value, and that it's ok to just cancel the update and plow on ahead.  This will result in "Damn the torpedoes" code that will infinitely loop on any error acessing the database, such as a network connection failure.  The user of this code would see the application hang while it tried to add record after record to rs, or access record after record in rsadd.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2779670
You're right, but you can do the following:
1) Check the exact errornumber (con.Errors(n).NativeError) to see what was the underlying error and react in different ways, depending on the error.
2) Add a counter to the error handler, and abort after (let's say) 5 errors
0
 

Author Comment

by:Heinrichsa
ID: 2779688
Thanks all for your very insightfull comments. I wish I could give all of you the points. I think the point is that I did not use the error handling routine correctly.
JetScootr, I tried both method and found that the find method is much slower and getting more slow the bigger the table gets. the second method's performance stay constant and is fast.
Because you first pointed out the error handler's problem - here is the points.
Thanks again to all concerned.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question