选择一个表名称存储在另一个表中的表

时间:2019-11-14 19:25:40

标签: sql ms-access dynamic-tables

我有一个带有临时表的Access dba,其中存储了所有表名。
表名称:SourceTable
tbl名称字段:SourceID
关键字段:ReferenceID

我想创建一个SELECT查询,其中“ FROM table”实际上是基于SQL查询的动态表名称。与此类似:

Select * from 'select SourceID from SourceTable where ReferenceID=1';

1 个答案:

答案 0 :(得分:0)

您也许可以使用VBA做一些事情。

  • 将组合框放置在表单上从SourceTable中获取其项目
  • 在表单上使用您的参考ID添加文本字段
  • 使用以下On_Click代码在表单上添加按钮

选择表格名称,输入参考ID,然后单击按钮

    Private Sub cmdQuery_Click()
        Dim qdf As QueryDef 'Query definition containter
        Dim mySQL As String 'SQL Statement container
        Dim t As String     'table name containter
        Dim rid As String   'ReferenceID container

        'getting values for your table and ReferenceID
        t = cboTableNames.Value
        rid = txtRID.Value

       'building SQL string
       mySQL = "Select * from " & t & " WHERE ReferenceID=" & rid & ";"

       'Delete the query if it already exists
       DoCmd.DeleteObject acQuery, "NewQuery"

       'building query 
       Set qdf = CurrentDb.CreateQueryDef("NewQuery", mySQL)

       'running query
       DoCmd.OpenQuery qdf.Name

       'closing query
       qdf.Close

       'clearing memory
       Set qdf = Nothing
    End Sub