如下代码说明如何在 Jet 数据库中创建 AutoIncrement 列。
Sub CreateAutoIncrColumn()
Dim cat As New ADOX.Catalog
Dim tbl As New ADOX.Table
Dim col As New ADOX.Column
' 打开目录
cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Program Files\Microsoft Office\" & _
"Office\Samples\Northwind.mdb;"
With tbl
.Name = "MyContacts"
Set .ParentCatalog = cat
' 创建字段并将它们追加到新的 Table 对象中。
.Columns.Append "ContactId", adInteger
' 产生 ContactId 列和自动递加列
.Columns("ContactId").Properties("AutoIncrement") = True
.Columns.Append "CustomerID", adVarWChar
.Columns.Append "FirstName", adVarWChar
.Columns.Append "LastName", adVarWChar
.Columns.Append "Phone", adVarWChar, 20
.Columns.Append "Notes", adLongVarWChar
End With
cat.Tables.Append tbl
Set cat = Nothing
End Sub