Solved

Access Insert Query

Posted on 2011-09-20
8
416 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 7

Accepted Solution

by:
BusyMama earned 500 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

631 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