|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
需要添加如下引用
- Sub CreateAccessTable() '创建数据库表
- Dim mycat As New ADOX.Catalog
- Dim myTable As New ADOX.Table
- If Dir(ThisWorkbook.Path & "\mydatabase.mdb") = "" Then
- mycat.Create "Provider=Microsoft.ace.OLEDB.12.0;Jet OLEDB:Engine Type=6;" & _
- "Data Source=" & ThisWorkbook.Path & "\mydatabase.mdb" '创建数据库
- End If
- mycat.ActiveConnection = "Provider=Microsoft.ace.OLEDB.12.0;Data Source=" & _
- ThisWorkbook.Path & "\mydatabase.mdb" '激活连接
- myTable.Name = "测试表a1"
- mycat.Tables.Append myTable '追加表
- With myTable.Columns '追加字段
- .Append "编号", adInteger
- .Append "姓名", adVarWChar, 30
- .Append "出生日期", adDate
- .Append "年龄", adInteger
- .Append "婚姻状况", adBoolean
- End With
- myTable.Keys.Append "PrimaryKey", adKeyPrimary, "编号" '把“编号”字段设为主键
-
- myTable.Columns("姓名").Properties("Jet OLEDB:Allow Zero Length") = True '允许空
-
- mycat.Tables.Refresh '刷新表
- Set myTable = Nothing
- Set mycat.ActiveConnection = Nothing
- End Sub
复制代码 注意:如果Jet OLEDB:Engine Type=5,则创建的为2002-2003兼容版。扩展名并不重要,按上面代码,即便扩展名是MDB,仍为2007版,但是最好改过来
[ 本帖最后由 老伙计2008 于 2010-3-27 23:46 编辑 ] |
|