Default values for Access fields

Is there any way to set the default value of a field when you use the ALTER TABLE command to create new fields on an Access table? Currently it defaults to null, but I need it to be zero.
LVL 2
BergJCAsked:
Who is Participating?
 
DalinCommented:
BergJC,
Try this:

Dim MyDB As Database
Dim tdfMyTable As TableDef

Set MyDB = OpenDatabase("YourDBNAmeWithPath")
Set tdfMyTable = MyDB.TableDefs!YourTableNAme

tdfMyTable.Fields!PostalCode.DefaultValue = "0" ' Or 0 without quote, depend on how you define it

MyDB.Close
0
 
DennisHCommented:
Would it be possible to use DAO for this?  It's a lot easier that way.  Then after you create the fields in the table, you can select the Field object from the Fields collection of the table and set it's Default property.  
0
 
BergJCAuthor Commented:
I don't know if DAO would work...could you show me how this could be done?
0
 
DalinCommented:
You should replace "PostalCode" with your fieldName
    tdfMyTable.Fields!YourFieldNAmeHere.DefaultValue = 0 ' assume it is a number field
Regards
Dalin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.