Link to home
Start Free TrialLog in
Avatar of BrianBeck
BrianBeck

asked on

"Syntax error in INSERT INTO statement" error (VB6/ Jet)

Dear Gurus
I receive a "syntax error in INSERT INTO statement" error when executing the following statement, and would be grateful of your assistance with the correction.  I believe that the required text delimiters are in place.
        SQLStr1 = "INSERT INTO " & tbl & " IN '" & txt_Output_Db & _
            "' SELECT FROM '" & txt_db1 & "' WHERE POSTCODE = '" & pc & "'"
        DbsConnection1.Execute SQLStr1
Thanks in advance
Avatar of dqmq
dqmq
Flag of United States of America image

You need a list of columns between "SELECT" and "FROM".  You must have the same number of columns, with the same datatypes, in the same order as the target table.  However, if the source table and the target table have identical structures, then you can just do:  

 SQLStr1 = "INSERT INTO " & tbl & " IN " & txt_Output_Db & _
            " SELECT * FROM " & txt_db1 & " WHERE POSTCODE = '" & pc & "'"

Note that I have also removed what appear to be extraneous quote marks.

Avatar of BrianBeck
BrianBeck

ASKER

Hi dqmq
Thank you for your suggestions.  I've firstly replaced my SQL string with your edition.  I've also ensured that the table structure of the output database is exactly the same as the source database (e.g. using Access's 'paste as structure only' command).
However, after the above, I still get the INSERT INTO syntax error.  To assist, below is the SQL string generated from the code:
INSERT INTO DB_ALL IN D:\pc_test_struct.mdb SELECT FROM D:\pc_test.mdb WHERE POSTCODE = '2059
Can you help please?
fyi, the SQL string includes a trailing apostrophe
Hi dqmq
I've found a Microsoft article regarding reserved words:
http://support.microsoft.com/kb/248738/
I've checked the column names in the Access tables and am very sure that none are reserved.

By the way, given the importance of correct SQL syntax, and that SQL is used so frequently in software today, does Microsoft have a web site that allows me to upload an SQL string and fetch a detailed answer as to why syntax is incorrect?
ASKER CERTIFIED SOLUTION
Avatar of Markus Fischer
Markus Fischer
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi harfang - yes, the table names are exactly the same in both databases, and both tables have exactly the same structure.  You SQL string worked perfectly, thank you.