?
Solved

Insert multiple records into MS Access database

Posted on 2008-10-03
7
Medium Priority
?
2,105 Views
Last Modified: 2012-05-05
I need to import multiple records from an external database into an MS Access table.  I can't seem to come up with the right syntax for multiple records.

This works for a single record:
INSERT INTO tbl_Sample (field_1, field_2, field_3) VALUES
("one",1,#01/01/2001#);

This results in an error for multiple records:
INSERT INTO tbl_Sample (field_1, field_2, field_3) VALUES
("one",1,#01/01/2001#),
("two",2,#02/02/2002#),
("three",3,#03/03/2003#);
"Missing semicolon (;) at end of SQL statement."

Seems like the second example should work, but it doesn't.  Can't seem to come up with the correct systax.


0
Comment
Question by:PWoodruff
[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
7 Comments
 
LVL 18

Expert Comment

by:jmoss111
ID: 22637990
 INSERT INTO tblSample (field_1, field_2, field_3)  
  SELECT value1, value2, value3
   UNION ALL
   SELECT value1, value2, value3
   UNION ALL

jim
0
 
LVL 7

Expert Comment

by:Norush
ID: 22637996
Try this:
INSERT INTO tbl_Sample  (field_1, field_2, field_3)
	SELECT  "one",1,#01/01/2001#
	UNION
	SELECT  "two",2,#02/02/2002#
	UNION
	SELECT  "three",3,#03/03/2003#

Open in new window

0
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 300 total points
ID: 22638005
Unfortunately that won't work. You have to call the INSERT statement separately for each of your records.

However, if you can link the two databases together then you can use a subselect to do this:

INSERT INTO tblSample(Field1,Field2,Field3) SELECT Field1, Field2, Field FROM YourLinkedTable
0
Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

 
LVL 44

Expert Comment

by:GRayL
ID: 22638067
You cannot use that format to insert multiple records, it is for a single record only.  I presume you are linked to the table in the external database.  that being the case use this format:

INSERT INTO tbl_Sample (field_1, field_2, field_3)
SELECT fld1, fld2, fld3 FROM tblLinked
WHERE fld2 BETWEEN 1 AND 3;

BTW try to get into the habit of eliminating underscores or any other special characters including spaces from you table, field, and vba variable names. tblSampe is just as readable as tbl_Sample and much easier to type.

Press Alt+F11 to get to the VBA window, click Help, MS Visual Basic Help, Microsoft Jet SQL Reference, Data Manipulation Language, INSERT INTO Statement for more insight.
0
 

Author Comment

by:PWoodruff
ID: 22638381
I'm not linked to the external database, so the SELECT approach will not work.

A bit more info that I didn't think was germane, but perhaps is is:  The process we will be using is for the keeper of the external database to generate the SQL file on his end and then eMail it to me.  Ideally, I would like to be able to import it directly into MS Access.  Alternatively, I guess he could generate his output data as a .csv file and then I could use Access' Get External Data / Import, but I wanted a more streamlined approach.  Also, there may be the case where he will be exporting multiple records for more than one table into a single file for me to import.  Sure wish Access could import like MySQL!

GRayL:  Thanks for the tip regarding uderscore, etc.  It's hard to break old habits.
0
 
LVL 44

Assisted Solution

by:GRayL
GRayL earned 75 total points
ID: 22638432
Don't forget importing a CSV file can be as simple as a click on a form.  You write the code once and you can use it over, and over, and over...
0
 
LVL 44

Expert Comment

by:GRayL
ID: 22639296
When you get down to trying to decide whether a player has contributed, and you decide on 25 points, - thanks but no thanks.  I'm all or nothing - if we be in the same arena again;-)
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

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 article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
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 “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Suggested Courses

765 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