SQL SET DEFAULT not working in MS Access

Steztric picture Steztric · Dec 27, 2012 · Viewed 13.8k times · Source

Possible Duplicate:
DEFAULT clause in ALTER TABLE statement resulting in syntax error

I am trying to execute the following statement using a SQL query within MS Access;

ALTER TABLE [table] ALTER COLUMN [column] SET DEFAULT 'default value'

However, I get a dialog displaying the error Syntax error in ALTER TABLE statement.

And when I click OK it highlights the word DEFAULT. I also tried the following statement;

ALTER TABLE [table]
ADD CONSTRAINT [Default] DEFAULT 'default value' FOR [column]

And I get another error Syntax error in CONSTRAINT clause.

What is the correct syntax for setting a default value in MS Access? The db file is Access 2003 format.

Answer

HansUp picture HansUp · Dec 27, 2012

Support for DEFAULT was included in Access DDL with Jet 4 (Access 2000). However it can only be used in DDL executed from an ADO connection.

This worked with Access 2007.

CurrentProject.Connection.Execute "ALTER TABLE MyTable " & _
    "ALTER COLUMN field2 SET DEFAULT ""foo"";"

Note if your db file is Access 97 or earlier, you won't be able to set a field DEFAULT value from DDL.