VB如何获取一个ACCESS数据库中的所有用户数据表的表名列表?
如题,注意要求是用户数据表。
我的下面的方法不行:
for each tb in db.tabledefs
debug.print tb.name
next
[解决办法]
'************************************************** 方法 1
'要引用Microsoft DAO 3.6
Public Sub enumdatabase(dbpath As String)
Dim db As Database
Set db = dbengine.OpenDatabase(dbpath)
Dim Tables As TableDef, fld
For Each Tables In db.tabledefs
'这里可以取得表名Tables.name
'For Each fld In Tables.Fields
If Left(Tables.Name, 4) <> "MSys" Then Print Tables.Name '& "---" & fld.Name
'这里可以取得字段名fld.name
'Next fld
Next Tables
db.Close
Set db = Nothing
End Sub
'************************************************** 方法 2 ADODB
'引用 Microsoft ADO Ext 2.5 for ddl and security
'引用 Microsoft ActiveX Data Objects 2.5 Library
Private Sub Command1_Click()
call OpenConnection
End Sub
Sub OpenConnection()
Dim cnn As New ADODB.Connection
Dim cat As New ADOX.Catalog
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source= c:\bwscale.mdb;"
Set cat.ActiveConnection = cnn
For i = 0 To cat.Tables.Count - 1
If Left(cat.Tables(i).Name, 4) <> "MSys" Then Print cat.Tables(i).Name
Next i
End Sub
'******************************************************* 方法 3 ADODC
'引用 Microsoft ADO Ext 2.5 for ddl and security
'部件 Microsoft Ado Data Control 6.0(OLEDB)
Private Sub Command1_Click()
call OpenConnection
End Sub
Sub OpenConnection()
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "c:\bwscale.mdb;Persist Security Info=False"
Dim cat As New ADOX.Catalog
cat.ActiveConnection = Adodc1.ConnectionString
For i = 0 To cat.Tables.Count - 1
If Left(cat.Tables(i).Name, 4) <> "MSys" Then Print cat.Tables(i).Name
Next i
Adodc1.RecordSource = "select * from bwmain"
Set DataGrid1.DataSource = Adodc1
End Sub