开发者

VBA for Access 2003 - DDL for creating access file: setting the Autonumber data type

开发者 https://www.devze.com 2022-12-30 09:17 出处:网络
So I have the below VB that creates an access file in the default workspace, creates a table, create some fields in that table...just need to know the syntax for setting the first data type/field to a

So I have the below VB that creates an access file in the default workspace, creates a table, create some fields in that table...just need to know the syntax for setting the first data type/field to autonumber...GUID, Counter, etc will not work as in 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开发者_如何学Go 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 in this line: set fldMain = tblMain.CreateField("ID", dbInteger) i need to replace the dbInteger with something that VB reconizes as the autonumber property. i have tried GUID, Counter, Autonumber, AutoIncrement....unfortunately none of these work

anyone know the syntax I am missing here?

Thanks, Justin


See Creating an AutoNumber field from code at The access Web.

Here are the key lines from that 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

BTW, what you're doing is not DDL.

0

精彩评论

暂无评论...
验证码 换一张
取 消