查询Access数据库中的每个表?

时间:2013-01-14 14:32:29

标签: sql ms-access

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

始终感谢帮助

1 个答案:

答案 0 :(得分: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表格并保存存档日期等详细信息,因为您将在某个阶段遇到限制。