AutoNumber for DAO CreateField

From what I gather so far, it does not seem to be possible to create a field object (i.e. CreateField) with an AutoNumber datatype using Access VBA/DAO. Is this correct? If so, there's got to be a trick to get around it somehow, doesn't there?
My hunch is that the AutoNumber thing is part of one of those special Document objects that Access tags onto certain DAOs (i.e., custom property), and is specific only to Access, and not really supported solely at the DAO level. Am I on the right track? Completely off-base?
Who is Participating?
tomookConnect With a Mentor Commented:
Try this:

Sub CreateTableWithAutonumber(TableName As String)
    Dim db As Database
    Dim td As TableDef
    Dim fd As Field
    Set db = CurrentDB
    Set td = CurrentDB.CreateTableDef("tblTestAutonum")
    Set fd = td.CreateField("fldTestAutonum")
    fd.Type = dbLong
    ' Here is the real trick. Have to set the attributes.
    fd.Attributes = dbAutoIncrField
    td.Fields.Append fd
    db.TableDefs.Append td
    Set fd = Nothing
    Set td = Nothing
    Set db = Nothing
End Sub

mdolemanAuthor Commented:
Thanks Tomook, that's the answer; ashamed I didn't find that one myself.
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.