Link to home
Start Free TrialLog in
Avatar of jlarger
jlarger

asked on

Access Insert Query

I am trying to insert additional records into a table of my database, but only if they are actually new records. The records are individuals with the following fields: FirstName, LastName, Address, City, State, and Category

The records I want to insert are in an Excel 2007 spreadsheet (source). I am thinking I want to check if the Category matches, and if it does then check the destination for the last name (within that category). If the last name exists check for the first name (within that category with matching last names). If the first and last names match for a given category, skip that record in the source. If there is not a matching record in the destination, then add that source record to the destination table.

There will be way more matches that do not need inserted than the other way around. My source table may have up to 5000 records or so, and the destination table has around 50,000. Microsoft Access 2007, on Windows 7, with a MySQL table being used via ODBC connection. VBA solutions are acceptable as well.

This is going to have to be performed dozens of times with various source tables, each being a different Category.

Thanks in advance
Avatar of BusyMama
BusyMama
Flag of United States of America image

Can you make the SQL table have a composite primary key which includes Category, First Name and Last Name?  That would (I believe) automatically prevent any duplicates based on those fields from being loaded.

If that isn't possible, then it seems to me that Category should be your LAST check, because assuming the people on the Excel sheet can have the same category, Last Name would be a larger filter.
Oops ... clicked Submit too soon.  Also, I would link the Access database directly to the spreadsheet as a linked table and avoid the VBA to send the Excel data to Access.
Avatar of jlarger
jlarger

ASKER

I have not heard of composite keys or composite primary keys for that matter, I will have to look into it.
ASKER CERTIFIED SOLUTION
Avatar of BusyMama
BusyMama
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jlarger

ASKER

Your description of the solution seems spot on what I am looking for. Looking at the code though I do not understand it. Also there was an issue where category was listed twice. I deleted the duplicate and was left with the remaining code:
INSERT INTO MockSQLTable ( FirstName, LastName, Address, City, State, Category )
SELECT MockLinkedExcelSheet.FirstName, MockLinkedExcelSheet.LastName, MockLinkedExcelSheet.Address, MockLinkedExcelSheet.City, MockLinkedExcelSheet.State, MockLinkedExcelSheet.Category
FROM MockLinkedExcelSheet LEFT JOIN MockSQLTable ON (MockLinkedExcelSheet.FirstName=MockSQLTable.FirstName) AND (MockLinkedExcelSheet.LastName=MockSQLTable.LastName) AND (MockLinkedExcelSheet.Category=MockSQLTable.Category)
WHERE (((MockSQLTable.Category) Is Null));

Open in new window

Could you possibly step me through the logic of this?
Sure ...

INSERT INTO MockSQLTable - tells the system which table you are going to place the data
(FirstName, LastName, Address, City, State, Category) - tells the system which data fields in the table you are putting data into

Then when we are telling the system WHAT data to put into the MockSQLtable, we are "selecting" it from another existing table:

SELECT MockLinkedExcelSheet.FirstName, etc.  -- the number of fields here matches the fields we placed in the INSERT INTO statement, and in the same order; the reason the table name is in front of each field is because the fields in the MockSQLTable have the same name, so this differentiates which "FirstName", etc.

FROM MockLinkedExcelSheet (tells the system which data table to SELECT the data from)

LEFT JOIN -- this is the part of the statement that matches the two tables; even though we are only going to put into the MockSQLTable what isn't already there, we have to use some kind of statement to match.  A LEFT JOIN will always return data from one table, and only matching data from another table.  In our case, we are returning all the data from the MockLinkedExcelSheet and only the matching data (see the next statement for the match requirements) from MockSQLTable.

ON -- identifies ALL the fields that must match - in your case 3 fields must match so we have AND in between each one (all three must match)

WHERE -- this serves as a filter for the data and actually goes with the INSERT statement at the start of the code -- we want to insert the data where the category in MockSQLTable is null (doesn't exist).  This might seem confusing, because your requirement is that all 3 fields (First & Last Name, and Category).  Remember we did a LEFT JOIN?  That way we had all of the data from the MockLinkedExcelSheet and matching data from the MockSQLTable - the WHERE says, OK, look at all that data, and anywhere that the matching data doesn't have a "Category" then we need to insert only those records.  We could have used first name or last name as well, or all three, but only one was necessary.

I hope that helps, I know it's a bit wordy, but easier to explain in person!
Avatar of jlarger

ASKER

Absolutely phenomenal work. Thank you so much, that explanation was just great. Wish I could give you more points.

I am not familiar with join queries at all which is where my confusion was, primarily with that WHERE statement and the Category being Null. Makes perfect sense now.

Thanks again!
Avatar of jlarger

ASKER

Great help