views:

1893

answers:

5

hi, I am working on a small ap that uses vbscript to write to a ms access db everytime i use it (really for personal use only so i don't need to worry about sql injection). When i run it i keep getting a "syntax error in INSERT INTO statement". The connection string is correct because the db locks when its run. table name is ors. What am I doing wrong?

sql1="INSERT INTO ors  VALUES (,,'B223234','12/22/08')"
constring="Provider=Microsoft.Jet.OLEDB.4.0;
  Data Source=C:\Documents and Settings\me\My Documents\tracker.mdb;
    User Id=admin;Password=;" 
set con=createobject("adodb.connection")
con.open constring
con.execute sql1
con.close
+3  A: 

If I remember correctly I think dates in MS Access require # surrounding them instead of single quotes. Try changing your insert to

INSERT INTO ors VALUES (,,'B223234',#12/22/08#)

in addition you may have to specify blanks for the missing parameters

INSERT INTO ors VALUES (NULL,NULL,'B223234',#12/22/08#)

my MS Access knowledge is a bit rusty but give that a shot.

Scott Vercuski
You use NULL for missing values INSERT INTO ors VALUES (NULL,NULL,'B223234',#12/22/08#)
DJ
ah yeah ... there ya go .... like I said .... rusty .. must be gettin old ... thanks DJ
Scott Vercuski
+1 ... But can you update your answer, so non-comment readers will see the right solution? :) I'd do it myself but not repped up enough yet.
Oorang
very true ! thanks for the reminder ... just updated the post. Thanks !
Scott Vercuski
+1  A: 

You Need to specify the fields to Insert In:

Example: Insert Into Table1 (test1, test2, test3) Values ('1','2','3')

Jon
They are optional if you give values for all fields.
crashmstr
I didn't know that until reading this thread, and have a hard time understanding why anyone would ever do it that way. For a 4-column table, not such a big deal, but who wants to start counting commas for tables with more columns? I surely don't!
David-W-Fenton
+2  A: 

You need NULL for the "blank" values. You also need to use # surrounding a date instead of the single quotes for date fields.

If you don't want to specify all values, you could specify only the fields you want to set.

(Assuming date field, not text for date value)

This:

INSERT INTO ors VALUES (NULL,NULL,'B223234',#12/22/08#)

Or This:

INSERT INTO ors (Field3, Field4) VALUES ('B223234',#12/22/08#)

crashmstr
dates in MS Access require # surrounding them
João Guilherme
Unless that is a text field. The above code *works* with a table with four text fields. The question information does not specify.
crashmstr
thanks that solved it, needed the nulls good answers
Anthony
If that solved it than accept his answer - give the man some rep. :)
AnonJr
Glad to help out
crashmstr
love to but my rep is at ll and you need 15
Anthony
You should be able to accept an answer to your own question, but you are 21 now anyway :)
crashmstr
+1  A: 

INSERT INTO ors(field1, fiedl2) VALUES ('B223234',#12/22/08#)

or

INSERT INTO ors VALUES (NULL, NULL, 'B223234',#12/22/08#)

Might work ..

João Guilherme
+1  A: 

Also note that the date field must be in either mm/dd/yy format or in another unambiguous format such as mm/dd/yyyy, yyyy/mm/dd or yyyy-mm-dd. If someone runs their system in dd/mm/yy or yy/mm/dd this will cause wrong dates to be inserted.

See http://mvps.org/access/datetime/date0005.htm for a solution that works within Access.

I know you stated this is for personal use only but you should be aware of these details for the future.

Tony Toews