DDL to create TimeStamp field

winshent

Registered User.
Local time
Today, 13:54
Joined
Mar 3, 2008
Messages
162
Hi Guys

I'm trying to use DDL to add a DATE field as above with NOW() as the default value..

I am using the same syntax pretty much (Altering instead of Creating a table), but its not working..

The error I get is:

err.number = 3293
err.description = Syntax error in ALTER TABLE statement.

Here is the code.. Any ideas ?

Code:
CurrentDb.Execute "ALTER TABLE MyTable ADD COLUMN RecordCreatedDate DATE DEFAULT NOW()"
 
All of the following failed:

Code:
CurrentDb.Execute "ALTER TABLE Audit ADD RecordCreatedDate DATE NOW() ;"

Code:
CurrentDb.Execute "ALTER TABLE Audit ADD RecordCreatedDate SET DEFAULT NOW() ;"

Code:
CurrentDb.Execute "ALTER TABLE Audit ADD RecordCreatedDate SET DEFAULT 'NOW()' ;"

Code:
CurrentDb.Execute "ALTER TABLE Audit ADD COLUMN RecordCreatedDate SET DEFAULT NOW() ;"

Code:
CurrentDb.Execute "ALTER TABLE Audit ADD COLUMN RecordCreatedDate SET DEFAULT 'NOW()' ;"
 
Code:
CurrentDb.Execute "ALTER TABLE Audit ADD RecordCreatedDate DATE DEFAULT GETDATE();"
 
Code:
CurrentDb.Execute "ALTER TABLE Audit ADD RecordCreatedDate DATE DEFAULT GETDATE();"

Still doesn't work.

GETDATE is a SQL Server function.

You also need to specify what you are adding, in this case COLUMN, as opposed to CONSTRAINT etc..
 

Hi BlueIshDan

Thanks for helping out so far..

I tried the following two examples from this link..

Code:
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255) DEFAULT 'Sandnes'
)

and

Code:
ALTER TABLE Persons
ALTER COLUMN City SET DEFAULT 'SANDNES'

Both failed. Wondering if its a problem with my install. Does the CREATE TABLE statement work on your machine ?
 
Ok.. looks like the following code works..

Frustrating that I cannot set the default in DDL though.

Code:
CurrentDb.Execute "ALTER TABLE Audit ADD COLUMN RecordCreatedDate DATE ;"

CurrentDb.TableDefs("Audit").Fields("RecordCreatedDate").DefaultValue = "now()"
 

Users who are viewing this thread

Back
Top Bottom