获得ACCESS数据库的表及字段信息

标签:
accessvba字段表 |
分类: WORD/ACCESS更奇妙 |
前几天写了一个VBA程序,可以获得ACCESS数据库的表及字段信息。保存一下
用引用了ADOx
dbfullname = ThisWorkbook.Path & "\data_inf.mdb"
cat.ActiveConnection = "provider=microsoft.jet.oledb.4.0; data
source=" & dbfullname
Cells.Clear
Range("A1:F1") = Array("表名称", "表类型", "字段名称", "字段类型", "字段长度",
"字段描述")
i = 2
For Each
mytbl In cat.Tables
For Each myfield In mytbl.Columns
Sheet3.Cells(i, 1) = mytbl.Name
Sheet3.Cells(i, 2) = mytbl.Type
Sheet3.Cells(i, 3) = myfield.Name
lei = myfield.Type
Select Case lei
Case 20: myVBAConstant = "adBigInt"
Case 128: myVBAConstant = "adBinary"
Case 11: myVBAConstant = "adBoolean"
Case 8: myVBAConstant = "adBSTR"
Case 136: myVBAConstant = "adChapter"
Case 129: myVBAConstant = "adChar"
Case 6: myVBAConstant = "adCurrency"
Case 7: myVBAConstant = "adDate"
Case 133: myVBAConstant = "adDBDate"
Case 134: myVBAConstant = "adDBTime"
Case 135: myVBAConstant = "adDBTimeStamp"
Case 14: myVBAConstant = "adDecimal"
Case 5: myVBAConstant = "adDouble"
Case 0: myVBAConstant = "adEmpty"
Case 10: myVBAConstant = "adError"
下面是百度上关于ADOx的内容
ADOX 概述编辑
Microsoft ActiveX Data Objects Extensions for Data Definition
Language and Security (ADOX) 是对 ADO
对象和编程模型的扩展。它可用于创建、修改和删除模式对象。它还包括安全对象,可用于维护用户和组,以及授予和撤消对象的权限。要通过开发工具使用
ADOX,需要建立对 ADOX 类型库的引用"Microsoft ADO Ext. for DDL and
Security"。
ADOX 对象编辑
Catalog 包含描述数据源模式目录的集合。
Column 表示表、索引或关键字的列。
Group 表示在安全数据库内有访问权限的组帐号。
Index 表示数据库表中的索引。
Key 表示数据库表中的主关键字、外部关键字或唯一关键字。
Procedure 表示存储的过程。
Table 表示数据库表,包括列、索引和关键字。
User 表示在安全数据库内具有访问权限的用户帐号。
View 表示记录或虚拟表的过滤集。
ADOX 方法编辑
Append(Columns) 将新的 Column 对象添加到 Columns 集合。
Append(Groups) 将新的 Group 对象添加到 Groups 集合。
Append(Indexes) 将新的 Index 对象添加到 Indexes 集合。
Append(Keys) 将新的 Key 对象添加到 Keys 集合。
Append(Procedures) 将新的 Procedure 对象添加到 Procedures 集合。
Append(Tables) 将新的 Table 对象添加到 Tables 集合。
Append(Users) 将新的 User 对象添加到 Users 集合。
Append(Views) 将新的 View 对象添加到 Views 集合。
Create 创建新的目录。
Delete 删除集合中的对象。
所属程序集
COM-->Microsoft ADO Ext. 2.8 for DDL and Security(Access
97-03文件,mdb类型)
若要支持Access2007版本以上创建的accdb文件,就需要使用
COM-->Microsoft ADO Ext. 6.0 for DDL and Security
http://s14/mw690/001lK57Yzy767YfNn2t9d&690
Dim cat As New ADOx.Catalog
Dim mytbl As ADOx.Table
Dim myfield As ADOx.Column
Dim i%, lei%
Dim dbfullname As String
dbfullname = ThisWorkbook.Path &
"\data_inf.mdb"
cat.ActiveConnection =
"provider=microsoft.jet.oledb.4.0; data source=" &
dbfullname
Cells.Clear
Range("A1:F1") = Array("表名称", "表类型", "字段名称",
"字段类型", "字段长度", "字段描述")
i = 2
For Each mytbl In cat.Tables
For Each
myfield In mytbl.Columns
Sheet3.Cells(i, 1) =
mytbl.Name
Sheet3.Cells(i, 2) =
mytbl.Type
Sheet3.Cells(i, 3) =
myfield.Name
lei =
myfield.Type
If lei = 3 Then
Sheet3.Cells(i, 4) = "整数"
If lei = 7 Then
Sheet3.Cells(i, 4) = "日期"
If lei = 202 Then
Sheet3.Cells(i, 4) = "文本"
Sheet3.Cells(i, 5) =
myfield.DefinedSize
Sheet3.Cells(i, 6) =
myfield.Properties("Description")
i = i + 1
Next
myfield
Next mytbl
Set cat = Nothing
Set myfield = Nothing
http://s4/mw690/001lK57Yzy767YfW5Fh73&690
Option Explicit
Sub dosth()
End Sub
http://s4/mw690/001lK57Yzy767YfW5Fh73&690
2017-5-27更新
Sub dosth()
Dim cat As
New ADOx.Catalog
Dim mytbl As
ADOx.Table
Dim myfield
As ADOx.Column
Dim i%,
lei%
Dim
dbfullname As String
Dim
myVBAConstant$