VB语言获取ACCESS数据库表中字段名

下面直接用例子来介绍:VB如何获取ACCESS数据库表中的字段名称
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;DataSource=Access数据库名.mdb;Persist Security Info=False'

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

Sub getFieldName() 'this sub function can get the field name
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;PersistSecurity 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

(0)

相关推荐