Solved

Resume Next Question - Dup Record in Database Table

Posted on 2006-11-21
4
135 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
[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
  • 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

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 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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…

752 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