1.VB 如何获取ACCESS数据库中的所有表名。 2.VB 如何获得指定表的所有字段名称?

2024-11-20 22:41:19
推荐回答(1个)
回答1:

Sub getTableName()
Dim RS As ADODB.Recordset
Dim CN As ADODB.Connection
Set CN = New ADODB.Connection
CN.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Access数据库名.mdb;Persist Security Info=False"

Set RS = CN.OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, Empty))
Do Until RS.EOF
If Left(RS!table_name, 4) <> "MSys" Then
List1.AddItem RS!table_name
End If
RS.MoveNext
Loop
RS.Close
Set RS = Nothing
CN.Close
Set CN = Nothing
End Sub

Sub getFieldName()
Dim RS As ADODB.Recordset
Dim CN As ADODB.Connection
Dim FN As ADODB.Field
Set CN = New ADODB.Connection
Set RS = New ADODB.Recordset

CN.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=access.mdb;Persist Security Info=False"

RS.Open "表名", CN
For Each FN In RS.Fields
List2.AddItem FN.Name
Next
RS.Close
Set RS = Nothing
CN.Close
Set CN = Nothing
End Sub