Solved

Resume Next Question - Dup Record in Database Table

Posted on 2006-11-21
4
131 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

744 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

12 Experts available now in Live!

Get 1:1 Help Now