1
command.CommandText = "CREATE TABLE MyTable (" +
                "[Count] INT NOT NULL AUTO_INCREMENT PRIMARY KEY ," +
                "[TimeAndDate] TIMESTAMP NOT NULL ," +
                "[SerialNumber] VARCHAR( 14 ) NOT NULL ," +
                "[Result] BOOL NOT NULL ," +
                "UNIQUE ([TimeAndDate]))";

command.ExecuteNonQuery();

Code above flags syntax error exception, can you help me correct the query string? Thank you.

3 Answers 3

3

I'd suggest pasting the resulting query text into an Access query; it will tell you where the error is.

On my Access XP, pasting the resulting SQL gave a syntax error on AUTO_INCREMENT; it should be AUTOINCREMENT (see Access 2007 SQL data types) and be specified as a data type, not a constraint. BOOL also gave an error => use BIT

Result which worked:

CREATE TABLE MyTable (
               [Count] AUTOINCREMENT NOT NULL PRIMARY KEY ,
               [TimeAndDate] TIMESTAMP NOT NULL ,
               [SerialNumber] VARCHAR( 14 ) NOT NULL ,
               [Result] BIT NOT NULL ,
               UNIQUE ([TimeAndDate]));
Sign up to request clarification or add additional context in comments.

2 Comments

How can you do that in Access? Where did you paste the SQL statement?
Create a new query; View menu shows Design, Datasheet and SQL (or something of the kind). Choose SQL. Alternatively, you could paste it in a suitable command in the VBA/code editor (Alt-F11) immediate window (Ctrl-G), e.g. currentdb.Execute("bla") (where bla is the sql statement). For newer versions of access you might need to set a reference to the dao library though...
2

You need an ending parenthesis ).

Comments

1

Just after MyTable you use an open bracket "(", that you do not close.

To create tables in Access, I normally use ADOX, this prevents this kind of syntax errors.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.