Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Access Insert Query

Posted on 2011-09-20
8
Medium Priority
?
425 Views
Last Modified: 2013-11-27
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
0
Comment
Question by:jlarger
  • 4
  • 4
8 Comments
 
LVL 7

Expert Comment

by:BusyMama
ID: 36569306
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.
0
 
LVL 7

Expert Comment

by:BusyMama
ID: 36569323
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.
0
 

Author Comment

by:jlarger
ID: 36569361
I have not heard of composite keys or composite primary keys for that matter, I will have to look into it.
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 7

Accepted Solution

by:
BusyMama earned 2000 total points
ID: 36569605
Here is an Access database with a query that you could use to find the mismatches based on the criteria you provided, and update the SQL server (even without a composite primary key).

This basically looks at the Excel spreadsheet (link your spreadsheet to Excel), and looks at the SQL table (link your SQL table to Access), makes the comparison using a left outer join on the 3 fields you indicated and appends the records which are in Excel but don't have a match in SQL to SQL.
Database5.accdb
0
 

Author Comment

by:jlarger
ID: 36570176
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?
0
 
LVL 7

Expert Comment

by:BusyMama
ID: 36570797
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!
0
 

Author Comment

by:jlarger
ID: 36571073
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!
0
 

Author Closing Comment

by:jlarger
ID: 36571076
Great help
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Suggested Courses

972 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