0

某个数据库中的所有表都有确切的列,所以我想知道是否有一种方法可以一次查询所有表,以获得我知道每个表都会有的特定几列。我想这样做的原因是数据库中的表数量会不断增长,我不想每天都去更改我的查询以适应新表的名称。

一如既往地感谢帮助

4

1 回答 1

2

在这种情况下,请尝试 ADO:

Function ListTablesContainingField(SelectFieldName) As String
   'Tables returned will include linked tables
   'I have added a little error coding. I don't normally do that
   'for examples, so don't read anything into it :) 
   Dim cn As New ADODB.Connection
   Dim rs As ADODB.Recordset
   Dim strTempList As String

       On Error GoTo Error_Trap

       Set cn = CurrentProject.Connection

       'Get names of all tables that have a column called <SelectFieldName>
       Set rs = cn.OpenSchema(adSchemaColumns, _
       Array(Empty, Empty, Empty, SelectFieldName))

       'List the tables that have been selected
       While Not rs.EOF
           'Exclude MS system tables
           If Left(rs!Table_Name, 4) <> "MSys" Then
               strTempList = strTempList & "," & rs!Table_Name
           End If
           rs.MoveNext
       Wend

       ListTablesContainingField = Mid(strTempList, 2)

   Exit_Here:

       rs.Close
       Set cn = Nothing
       Exit Function

   Error_Trap:

       MsgBox Err.Description
       Resume Exit_Here
   End Function

来自:http ://wiki.lessthandot.com/index.php/ADO_Schemas

您可能想考虑一张表格,如果您还没有表格,列出链接的 Excel 表格并保存存档日期等详细信息,因为您会在某个阶段遇到限制。

于 2013-01-14T15:14:42.873 回答