DefaultValue Property (MDB)

DefaultValue Property (MDB)

In Microsoft Access, if you set the DAO DefaultValue property to GenUniqueID( ) to create an AutoNumber field, you must also set the Attributes property to dbAutoIncrement. If you don't set the Attributes property in this way, Microsoft Access won't assign the AutoNumber data type to the field, and you'll get an error when you try to view the table in Datasheet view.

The following code shows how you can create a field with the AutoNumber data type, given a TableDef object variable tdf and a Field object variable fld.

Set fld = tdf.CreateField("EmployeeID", dbLong)
fld.DefaultValue = "GenUniqueID()"
fld.Attributes = dbAutoIncrField
tdf.Fields.Append fld

If the DAO DefaultValue property setting is an expression, it can't contain user-defined functions, Microsoft Access domain aggregate functions, SQL aggregate functions, the CurrentUser function, the Eval function, or references to queries, forms, or other Field objects.