Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
642 views
in Technique[技术] by (71.8m points)

sql - How do I create a decimal field in Access with Alter Table?

I want to programmatically create a new column in an MS Access table. I've tried many permutations of ALTER TABLE MyTable Add MyField DECIMAL (9,4) NULL; and got:

Syntax Error in Field Definition

I can easily create a number field that goes to a Double type, but I want decimal. I would very strongly prefer to do this in a single ALTER TABLE statement and not have to create a field and then alter it.

I am using Access 2003.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The decimal data type isn't supported in the default Jet 4.0 mdb file. You have to use the SQL Server compatibility syntax (ANSI 92) setting to use the decimal data type in the SQL Window.

Click on the menu, Tools > Options. Click on the Tables/Query tab. Mark the check box for "This database" in the SQL Server compatibility syntax (ANSI 92) section. This mode will affect the entire db, including queries with wildcards, so you may want to try this on a copy of your db.

Paste this into the SQL window:

ALTER TABLE MyTable
  Add COLUMN MyField DECIMAL (9,4) NULL;

If you don't want to alter the mode of your database, you must use vba code with the adodb library:

Dim conn As ADODB.Connection

Set conn = CurrentProject.Connection
conn.Execute "ALTER TABLE MyTable " _
    & "ADD COLUMN MyField DECIMAL (9,4) NULL;"
conn.Close

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...