Microsoft SQL Server和MySQL有一个我可以查询的INFORMATION_SCHEMA表.但是,它在MS Access数据库中不存在.
有没有相同的我可以使用?
要建立在Ilya的答案上,请尝试以下查询:
SELECT MSysObjects.Name AS table_name FROM MSysObjects WHERE (((Left([Name],1))<>"~") AND ((Left([Name],4))<>"MSys") AND ((MSysObjects.Type) In (1,4,6))) order by MSysObjects.Name
(这个没有修改的MDB工作)
ACCDB用户可能需要做这样的事情
SELECT MSysObjects.Name AS table_name FROM MSysObjects WHERE (((Left([Name],1))<>"~") AND ((Left([Name],4))<>"MSys") AND ((MSysObjects.Type) In (1,4,6)) AND ((MSysObjects.Flags)=0)) order by MSysObjects.Name
因为有一个额外的表,似乎是某种系统表.
您可以在Access中使用模式.
Sub ListAccessTables2(strDBPath) Dim cnnDB As ADODB.Connection Dim rstList As ADODB.Recordset Set cnnDB = New ADODB.Connection ' Open the connection. With cnnDB .Provider = "Microsoft.Jet.OLEDB.4.0" .Open strDBPath End With ' Open the tables schema rowset. Set rstList = cnnDB.OpenSchema(adSchemaTables) ' Loop through the results and print the ' names and types in the Immediate pane. With rstList Do While Not .EOF If .Fields("TABLE_TYPE") <> "VIEW" Then Debug.Print .Fields("TABLE_NAME") & vbTab & _ .Fields("TABLE_TYPE") End If .MoveNext Loop End With cnnDB.Close Set cnnDB = Nothing End Sub
来自:http://msdn.microsoft.com/en-us/library/aa165325(office.10).aspx
这是一个更新的答案,适用于使用数据访问对象(DAO)的Access 2010 VBA.表的名称保存在TableDef.Name中.所有表定义的集合都保存在TableDefs中.以下是循环表名称的快速示例:
Dim db as Database Dim td as TableDef Set db = CurrentDb() For Each td In db.TableDefs YourSubTakingTableName(td.Name) Next td