Solved

ADO errors

Posted on 2000-05-04
10
147 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
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 6

Expert Comment

by:setiawan
Comment Utility
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
Comment Utility
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 200 total points
Comment Utility
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
 
LVL 2

Expert Comment

by:JetScootr
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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
Comment Utility
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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now