VBA for Access 2003 - DDL to Create Access File: Set Data Type to Autonumber

So I have the following VB that creates an access file in the default workspace, creates a table, creates some fields in that table ... just need to know the syntax to set the first data type / field for the autonumber..GUID, Counter, etc. .d. Will not work like Access SQL

' error handling usually goes here

dim ws as workspace
dim dbExample as database
dim tblMain as TableDef
dim fldMain as Field
dim idxMain as Index

set ws = workspace(0)

set dbExample = ws.CreateDatabase('string file path')

set tblMain = dbExample.CreateTableDef("tblMain")

set fldMain = tblMain.CreateField("ID", 'right here I do not know what to substitute for dbInteger to get the autonumber type to work )

tblMain.Fields.Append fldMain
etc to create other fields and indexes

      

so on this line: set fldMain = tblMain.CreateField ("ID", dbInteger) I need to replace dbInteger with VB renaming as autonumber property. i have tried GUID, Counter, Autonumber, AutoIncrement .... unfortunately none of these work

Does anyone know the syntax I'm missing here?

Thanks Justin

+2


a source to share


1 answer


See Create an AutoNumber Field from Code on the Access Site.

Here are the key lines on this linked page:



Set db = Application.CurrentDb
Set tdf = db.TableDefs(strTableName)
' First create a field with datatype = Long Integer '
Set fld = tdf.CreateField(strFieldName, dbLong)
With fld
    ' Appending dbAutoIncrField to Attributes '
    ' tells Jet that its an Autonumber field '
    .Attributes = .Attributes Or dbAutoIncrField
End With
With tdf.Fields
    .Append fld
    .Refresh
End With

      

By the way, you are not doing DDL.

+1


a source







All Articles