PWoodruff
asked on
Insert multiple records into MS Access database
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.
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.
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#
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
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.
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;-)
SELECT value1, value2, value3
UNION ALL
SELECT value1, value2, value3
UNION ALL
jim