0

I created a MS Access database at runtime and tried to create a table. Below code is showing the error "Syntax error in CREATE TABLE statement" while creating a table at runtime.

cmmd.CommandText = "CREATE TABLE tblContacts( [SectionID] AUTOINCREMENT PRIMARY KEY,[ScetionName] Text(50), [CatID] Number(Integer), [Rate] Number(Double), [Prefix] Text(5), [Suffix] Text(5), [NextNumber] Number(Integer), [Inactive] Yes, [ModUserID] Number(Integer),[ModDate] Date)";
cmmd.ExecuteNonQuery();
2

1 Answer 1

2

The Access db engine will balk at field type declarations such as Number(Integer). Assuming you will execute the statement from an OleDb connection, use this one ...

cmmd.CommandText = "CREATE TABLE tblContacts( [SectionID] COUNTER PRIMARY KEY,[ScetionName] Text(50), [CatID] Long, [Rate] Double, [Prefix] Text(5), [Suffix] Text(5), [NextNumber] Number(Integer), [Inactive] YesNo, [ModUserID] Long,[ModDate] DateTime)";

You can find a table which includes valid Access DDL field type declarations here: Field type reference - names and values for DDL, DAO, and ADOX

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @HansUp. It worked well. Please help me in doing relationships. The CatID should be from the Category table? How to relate it CatID field to tblCategory?
Use the REFERENCES key word. See here for an example --- look at the example under "Create the Booking table". Post a new question if you run into trouble.

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.