Solved

ADO errors

Posted on 2000-05-04
10
154 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
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 200 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 142

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 142

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

910 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

16 Experts available now in Live!

Get 1:1 Help Now