• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 25
  • Last Modified:

How insert new record in a table if there is no perfect match of 10 fields

I have a temporary table that contains several fields.  But I want to add records to a separate table named tblVendors using just 10 of the fields in the temporary table.  The code will have to loop through the records in the temporary table and insert a new record into tblVendors BUT ONLY if the same combination of field data does not already exist.  What would this VBA code look like?  

In other words if any one field is different that a record already in tblVendors, go ahead and create the new record.  But if all 10 of them are an exact match, do not create the new record

--Steve
0
SteveL13
Asked:
SteveL13
  • 2
  • 2
1 Solution
 
PatHartmanCommented:
You don't need code at all.  Create a compound unique index on the ten fields (lucky for you because 10 is the maximum that Jet/ACE support)  SQL Server is more flexible.

To make a compound index,
1. Open the table in design view
2. Open the indexes dialog
3. On the first completely empty line,give the index a name (I use UniqueIDX), select the first field, and choose the unique option
4. For fields 2-10, just enter the field name on a separate line.
5. At the end, the index will contain 10 lines.  The first will have a name but the name field will be empty on all the rest.  This tells Access that all the columns together will make up the unique index.

Then run an append query that selects the data from table1 to table2.  When the append query finishes, you'll get a message that tells you that x records were appended and y records were not appended due to key violations, and z records were not appended due to rule violations.
0
 
SteveL13Author Commented:
WOW!  Perfect.  The only other think I had to do was make UniqueIDX = Yes.  Thank you.
0
 
PatHartmanCommented:
and choose the unique option
That's what I said :)
0
 
SteveL13Author Commented:
I understand now.   Thanks again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now