Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to specify default in SQL Create TABLE?

20 views
Skip to first unread message

AA2e72E

unread,
Dec 6, 2004, 3:59:05 AM12/6/04
to
Is there any way to specify field defaults in SQL Create table? E.G. with
CREATE TABLE [MYTABLE] (MYFIELD DECIMAL(20,2))
how do I specify the field MYFIELD to have a default value of 100.75?

Allen Browne

unread,
Dec 6, 2004, 6:38:50 AM12/6/04
to
In JET 4 (Access 2000 and later), you can use the DEFAULT keyword to specify
a literal value (but not a function AFAIK).

You may find that this does not work in the query window, and you have to
execute it under ADO. Example:
strSql = "CREATE TABLE [MYTABLE] (MYFIELD DECIMAL(20,2) DEFAULT
100.75);"
CurrentProject.Connection.Execute strSql

You probably need to know that Access is incapable of handling fields of
type Decimal correctly. Details in:
Incorrect Sorting (Decimal fields)
at:
http://members.iinet.net.au/~allenbrowne/bug-08.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"AA2e72E" <AA2...@discussions.microsoft.com> wrote in message
news:E0028886-1979-48CD...@microsoft.com...

AA2e72E

unread,
Dec 6, 2004, 7:45:01 AM12/6/04
to
Thanks Allen. That works and it does populate the Default field in Design View.
How do you set the Validation Rule?

Allen Browne

unread,
Dec 6, 2004, 9:07:45 AM12/6/04
to
AFAIK, you cannot set a Validation Rule using a DDL query statement.

Use DAO, like this:
CurrentDb().TableDefs("MyTable").Fields("MyField").ValidationRule =
"Between 1 and 100"

For this particular property, you could also use ADOX to set the "Jet
OLEDB:Column Validation Rule" Property of the Column in the Table in the
Catalog. However, ADOX is incomplete, inconsistent between versions, buggy,
and subject to reference problems so we use it only where we are absolutely
forced to.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"AA2e72E" <AA2...@discussions.microsoft.com> wrote in message

news:79A0DC44-895E-4A2E...@microsoft.com...


> Thanks Allen. That works and it does populate the Default field in Design
> View.
> How do you set the Validation Rule?
>
>
> "Allen Browne" wrote:
>
>> In JET 4 (Access 2000 and later), you can use the DEFAULT keyword to
>> specify
>> a literal value (but not a function AFAIK).
>>
>> You may find that this does not work in the query window, and you have to
>> execute it under ADO. Example:
>> strSql = "CREATE TABLE [MYTABLE] (MYFIELD DECIMAL(20,2) DEFAULT
>> 100.75);"
>> CurrentProject.Connection.Execute strSql
>>
>> You probably need to know that Access is incapable of handling fields of
>> type Decimal correctly. Details in:
>> Incorrect Sorting (Decimal fields)
>> at:
>> http://members.iinet.net.au/~allenbrowne/bug-08.html
>>
>>

0 new messages