Solved

Access Insert Query

Posted on 2011-09-20
8
403 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
Comment Utility
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
Comment Utility
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
Comment Utility
I have not heard of composite keys or composite primary keys for that matter, I will have to look into it.
0
 
LVL 7

Accepted Solution

by:
BusyMama earned 500 total points
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:jlarger
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Great help
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

771 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

11 Experts available now in Live!

Get 1:1 Help Now