简体   繁体   English

在 Ms-Access 中将表名作为查询参数传递

[英]Passing a table name as a query parameter in Ms-Access

I'm working on a access database that is composed of multiple tables with the same structure.我正在研究一个由多个具有相同结构的表组成的访问数据库。 What I am trying to do is use a combo box on a form to select the table and then execute a query with the chosen table name.我想要做的是使用表单上的组合框来选择表,然后使用所选的表名执行查询。

The query would be the same except for the table name that's being used.除了正在使用的表名之外,查询将是相同的。

I'm having trouble passing the table name from the combo box to the query.我在将表名从组合框中传递给查询时遇到问题。

I know its probably not the best database structure but its what I need to do.我知道它可能不是最好的数据库结构,但它是我需要做的。 If anyone has any advice to share that would be great!如果有人有任何建议可以分享,那就太好了!

Thanks!谢谢!

You cannot simply do that using a pre compiled SQL Query.您不能简单地使用预编译的 SQL 查询来做到这一点。 You need a little bit of VBA to get it going, this is how you would do it.你需要一点 VBA 才能让它运行起来,这就是你要做的。

Create a Form with one ComboBox and one button.创建一个带有一个 ComboBox 和一个按钮的表单。

Name the ComboBox as tableNameCombo and the button as runQueryBtn .将 ComboBox 命名为tableNameCombo并将按钮命名runQueryBtn Save the Form, with the name frm_QueryRun .保存表单,名称为frm_QueryRun

Create a new Query something along the lines of,创建一个新的 Query 类似的东西,

SELECT * FROM randomTableName;

Save this as qry_Tmp .将其另存为qry_Tmp

Now go back to Form design, then on Property sheet of the Form , look for the Current Method.现在回到表的设计,然后在属性表形式,请查找最新的方法。 Then paste the following code into the Form Current.然后将以下代码粘贴到 Form Current 中。

Note : If this is your First VBA, check out : http://www.baldyweb.com/FirstVBA.htm注意:如果这是您的第一个 VBA,请查看: http : //www.baldyweb.com/FirstVBA.htm

Private Sub Form_Current()
    Dim tblStr As String
    Dim dbObj As DAO.Database, tdObj As DAO.TableDef

    Set dbObj = CurrentDB()

    Me.tableNameCombo.RowSourceType = "Value List"
    For Each tdObj In db.TableDefs
        If Left(tdObj.Name, 4) <> "MSys" Then tblStr = tblStr & tdObj.Name & ";"
    Next

    tblStr = Left(tblStr, Len(tblStr)-1)
    Me.tableNameCombo.RowSource = tblStr
    Set dbObj = Nothing
End Sub

Once this is done, you would need to construct your qry_Tmp.完成此操作后,您将需要构建您的 qry_Tmp。 something like, on the click of the button.类似的,点击按钮。

Private Sub runQueryBtn_Click()
    Dim dbObj As DAO.Database, qdObj As DAO.QueryDef
    If Me.tableNameCombo.ListIndex = -1 Then
        MsgBox "Table Name needs to be selected, before continuing.", vbCritical
        Exit Sub
    End If

    Set dbObj = CurrentDB()
    Set qdObj = dbObj.QueryDefs("qry_Tmp")

    qdObj.SQL = "SELECT " & Me.tableNameCombo & ".* FROM " & Me.tableNameCombo & ";"

    qdObj.Execute dbFailOnError

    qdObj.Close
    Set qdObj = Nothing
    Set dbObj = Nothing
End Sub

Save the Form, Close it, compile the code for any error.保存表单,关闭它,编译任何错误的代码。 Then run the code.然后运行代码。 Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM