Dim cat As ADOX.Catalog
Dim tbl As ADOX.Table
Dim PK As ADOX.Index
Set cat = New ADOX.Catalog
set cat.ActiveConnection=cnn
Set tbl = New ADOX.Table
Set tbl.ParentCatalog = cat
With tbl
.Name = "aaa"
.Columns.Append "f1", adVarWChar, 20
.Columns.Append "f2", adInteger
'设置f1为主键
Set PK = New ADOX.Index
PK.Name = "PK"
PK.PrimaryKey = True
PK.Columns.Append "f1"
.Indexes.Append PK
Set PK = Nothing
End With
cat.Tables.Append tbl
Set tbl = Nothing
先引用Microsoft ADO Ext. 2.x for DDL and Security
Private Function IsPriKey(ByRef cnn As Connection, ByVal TableName As String, ByVal FieldName As String) As Boolean
Dim cat As ADOX.Catalog
Dim k As ADOX.Key
Dim c As ADOX.Column
Set cat = New Catalog
Set cat.ActiveConnection = cnn
FieldName = UCase$(FieldName)
For Each k In cat.Tables(TableName).Keys
If k.Type = adKeyPrimary Then
For Each c In k.Columns
If UCase$(c.Name) = FieldName Then
IsPriKey = True
Exit Function
End If
Next c
End If
Next k
IsPriKey = False
End Function