return 包含 excel 宏的所有访问 (.mdb) 表的列表
return a list of all access (.mdb) tables with an excel macro
我在 Sheet1 范围 B1 中定义了一个 mdb 文件位置。这个值为:
"C:\Users\User\Desktop\Test.mdb"
我要做的是生成此文件中所有 table 和 excel 中的所有 return 的列表。我有一个部分工作的脚本,但它正在 return 不需要的项目
我正在解决这个问题:
Sub GetTableNames()
Dim cnn As ADODB.Connection
Dim cat As ADOX.Catalog
Dim tbl As ADOX.Table
Dim lRow As Long
Dim szConnect As String
LastRowSetup = Worksheets("Setup").Cells(Rows.Count, 1).End(xlUp).Row 'last row where table names populate
If LastRowSetup < 10 Then
LastRowSetup = 10 'so we dont accidentally clear important data above this
End If
Sheets("Setup").Range("A10:A" & LastRowSetup & "").ClearContents 'clear old data
fStr = Sheets("Setup").Range("C2").Value 'file location of mdb
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & fStr & ";"
Set cnn = New ADODB.Connection
cnn.Open szConnect
Set cat = New ADOX.Catalog
Set cat.ActiveConnection = cnn
lRow = 10
For Each tbl In cat.Tables
Sheet1.Cells(lRow, 1).Value = tbl.Name
lRow = lRow + 1
Next tbl
cnn.Close
Set cat = Nothing
Set cnn = Nothing
End Sub
然而,return还有许多不是 table 名称的其他内容。例如
~TMPCLP313341
~TMPCLP74661
Approved_table1
Approved_table2
MSysAccessStorage
MSysAccessXML
MSysACEs
MSysNameMap
MSysNavPaneGroupCategories
MSysNavPaneGroups
MSysNavPaneGroupToObjects
MSysNavPaneObjectIDs
MSysObjects
MSysQueries
MSysRelationships
当我打开 mdb 时,我看到的只有 'table 1' 和 'table 2'。有没有办法在 ADODB 连接中实现一个额外的过滤器,而不是 return 所有的临时文件和 Msys 对象,或者这是我只需要在导入后过滤的东西。
请注意,我必须设置对 Microsoft 的引用
ADO 分机。 2.X 用于 DDL 和安全对象库以及普通的 ADO
对象库。
试试这个代码:
For Each tbl In cat.Tables
If Left(tbl.Name, 4) <> "MSys" And Left(tbl.Name, 1) <> "~" then
Sheet1.Cells(lRow, 1).Value = tbl.Name
lRow = lRow + 1
end if
Next tbl
is there a way to implement an additional filter in the ADODB connection to not return all of the temp files and Msys objects or is this something i will just have to filter after importing.
不,您将不得不遍历 table 名称并简单地忽略系统 ("MSys...") 和临时 ("~...") tables。
我在 Sheet1 范围 B1 中定义了一个 mdb 文件位置。这个值为:
"C:\Users\User\Desktop\Test.mdb"
我要做的是生成此文件中所有 table 和 excel 中的所有 return 的列表。我有一个部分工作的脚本,但它正在 return 不需要的项目
我正在解决这个问题:
Sub GetTableNames()
Dim cnn As ADODB.Connection
Dim cat As ADOX.Catalog
Dim tbl As ADOX.Table
Dim lRow As Long
Dim szConnect As String
LastRowSetup = Worksheets("Setup").Cells(Rows.Count, 1).End(xlUp).Row 'last row where table names populate
If LastRowSetup < 10 Then
LastRowSetup = 10 'so we dont accidentally clear important data above this
End If
Sheets("Setup").Range("A10:A" & LastRowSetup & "").ClearContents 'clear old data
fStr = Sheets("Setup").Range("C2").Value 'file location of mdb
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & fStr & ";"
Set cnn = New ADODB.Connection
cnn.Open szConnect
Set cat = New ADOX.Catalog
Set cat.ActiveConnection = cnn
lRow = 10
For Each tbl In cat.Tables
Sheet1.Cells(lRow, 1).Value = tbl.Name
lRow = lRow + 1
Next tbl
cnn.Close
Set cat = Nothing
Set cnn = Nothing
End Sub
然而,return还有许多不是 table 名称的其他内容。例如
~TMPCLP313341
~TMPCLP74661
Approved_table1
Approved_table2
MSysAccessStorage
MSysAccessXML
MSysACEs
MSysNameMap
MSysNavPaneGroupCategories
MSysNavPaneGroups
MSysNavPaneGroupToObjects
MSysNavPaneObjectIDs
MSysObjects
MSysQueries
MSysRelationships
当我打开 mdb 时,我看到的只有 'table 1' 和 'table 2'。有没有办法在 ADODB 连接中实现一个额外的过滤器,而不是 return 所有的临时文件和 Msys 对象,或者这是我只需要在导入后过滤的东西。
请注意,我必须设置对 Microsoft 的引用 ADO 分机。 2.X 用于 DDL 和安全对象库以及普通的 ADO 对象库。
试试这个代码:
For Each tbl In cat.Tables
If Left(tbl.Name, 4) <> "MSys" And Left(tbl.Name, 1) <> "~" then
Sheet1.Cells(lRow, 1).Value = tbl.Name
lRow = lRow + 1
end if
Next tbl
is there a way to implement an additional filter in the ADODB connection to not return all of the temp files and Msys objects or is this something i will just have to filter after importing.
不,您将不得不遍历 table 名称并简单地忽略系统 ("MSys...") 和临时 ("~...") tables。