Solved

Resume Next Question - Dup Record in Database Table

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

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

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…

920 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