一直以来都用SQL语句来操作Access,但发现有些Access字段的属性不能用SQL来操作,在网上搜索了一些资料后,才了解了一些用ADOX设定字段的方法,为了方便了解这些字段的属性,就先自己建立一个Access数据库,然后创建好表与字段,设置好这字段的属性,然后后用下面的程序把它的属性例出到Excel中来,此程序需要引用ADOX。 |
Sub 获取Access数据库字段的所有属性() |
----------------------------------- |
|
Dim mycat As ADOX.Catalog |
Dim MyTab As ADOX.Table |
Dim MyCol As ADOX.Column |
Dim MyPro As ADOX.Property |
Dim tSh As Worksheet |
Dim i As Long |
Dim DataName As String, PassStr As String, TableName As String |
DataName = "Excel吧" '数据库名称 |
DataName = ThisWorkbook.Path & "\" & DataName & ".mdb" |
If Dir(DataName) = "" Then |
MsgBox "数据库:" & DataName & "不存在!" |
Exit Sub |
End If |
PassStr = "" '数据库密码 |
TableName = "数据表2" '数据表名称 |
Set tSh = ThisWorkbook.Worksheets.Add |
tSh.Range("A11") = Array("字段名称", "字段类型", "字段属性", "属性值") |
Set MyCat = New ADOX.Catalog |
MyCat.ActiveConnection = "rovider=Microsoft.Jet.Oledb.4.0;Data Source=" & _ |
DataName & ";Jet OLEDB:Database Password=" & MyPass |
|
Set MyTab = New ADOX.Table |
Set MyTab = MyCat.Tables(TableName) |
i = 2 |
For Each MyCol In MyTab.Columns |
tSh.Cells(i, 1) = MyCol.Name |
tSh.Cells(i, 2) = MyCol.Type |
For Each MyPro In MyCol.Properties |
tSh.Cells(i, 3) = MyPro.Name |
tSh.Cells(i, 4) = MyPro.Value |
i = i + 1 |
Next |
Next |
tSh.Cells.EntireColumn.AutoFit |
MsgBox "字段信息读取完毕!", , "提示" |
End Sub |
请自己修改程序中的数据库名,密码与数据表名,路径为包含程序的Excel文件的目录中。 |
从程序的运行结果可以看到,列举的属性有: |
0 Autoincrement 自动编号 |
1 Default 默认值 |
2 Description |
3 Nullable 必填字段 |
4 Fixed Length |
5 Seed |
6 Increment |
7 Jet OLEDB:Column Validation Text 有效性文本 |
8 Jet OLEDB:Column Validation Rule 有效性规则 |
9 Jet OLEDB:IISAM Not Last Column |
10 Jet OLEDB:AutoGenerate |
11 Jet OLEDB:One BLOB per Page |
12 Jet OLEDB:Compressed UNICODE Strings |
13 Jet OLEDB:Allow Zero Length 允许空字符串 |
14 Jet OLEDB:Hyperlink 超链接型 |