Solved

Resume Next Question - Dup Record in Database Table

Posted on 2006-11-21
4
133 Views
Last Modified: 2010-04-30
Hello all.  I have some VB code that loops through and inserts records into an access table.   I get to a record that already exists in my table and access throws a error change could not be inserted because of dup key.  Then VB throws a file already open error on the .Update line of the below code.  Should I just do a Resume Next or something?  I dont want to check the DB everytime with a select statement.  Any ideas?

With dbAccessDb.OpenRecordset("tblCustomer", dbOpenDynaset)
                        .AddNew
                        .Fields!Acct = Acct                        
                        .Fields!Cust = Cust
                        .Update
                        .Close
                    End With
0
Comment
Question by:sbornstein2
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 125 total points
ID: 17989624
I wouldn't simply resume next. What if it is a different error? How would you trap it?

Rather create an error handler - eg.

On Error Goto MY_ERR:

With dbAccessDb.OpenRecordset("tblCustomer", dbOpenDynaset)
                        .AddNew
                        .Fields!Acct = Acct                        
                        .Fields!Cust = Cust
                        .Update
                        .Close
                    End With

MY_ERR:
If err.Number<>0 then
  if err.Number=x then
   'do nothing because I don't care about duplicates being rejected
  else
      MsgBox Err.Description
  endif
endif
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 17989628


Something Like ...


On Error GoTo ErrHandler:

your loop .....

With dbAccessDb.OpenRecordset("tblCustomer", dbOpenDynaset)
  .AddNew
  .Fields!Acct = Acct
  .Fields!Cust = Cust
  .Update
  .Close
End With

ErrHandler:
Resume Next

your loop .....
0
 

Author Comment

by:sbornstein2
ID: 17989649
i dont want the overhead of having to check the database each time before the insert
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17989689
You wouldn't be - you would just handle the error on the insert and resume IF the error was a PK violation
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
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…

821 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