111,073
社区成员




create table [test] (id integer identity(1,1) primary key,Description text(50))
'引用 COM 中的 Microsoft ActiveX Data Objects 2.8 Library
'引用 Microsoft ADO Ext. 2.8 for DDL and Security
Sub CreateAccessTable(ByVal DbName As String,ByVal TblName As String)
Try
Dim cat As New ADOX.CatalogClass()
Dim AdoConn As New ADODB.Connection
AdoConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DbName & ";Jet OLEDB:Database Password=;"
AdoConn.Open()
cat.ActiveConnection = AdoConn
Dim tbl As New ADOX.TableClass()
tbl.ParentCatalog = cat
tbl.Name = TblName
'增加一个自动增长的字段
Dim col As New ADOX.ColumnClass()
col.ParentCatalog = cat
col.Type = ADOX.DataTypeEnum.adInteger
' 必须先设置字段类型
col.Name = "id"
col.Properties("Jet OLEDB:Allow Zero Length").Value = False
col.Properties("AutoIncrement").Value = True
tbl.Columns.Append(col, ADOX.DataTypeEnum.adInteger, 0)
'增加一个文本字段
Dim col2 As New ADOX.ColumnClass()
col2.ParentCatalog = cat
col2.Name = "Description"
tbl.Columns.Append(col2, ADOX.DataTypeEnum.adVarChar, 50)
'设置主键
tbl.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "id", "", "")
cat.Tables.Append(tbl)
tbl = Nothing
cat = Nothing
MsgBox("数据库表:" + TblName + "已经创建成功!")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub