ACCESS VBA 从列表框中选择多个值并执行查询名称

ACCESS VBA selecting multiple values from listbox and executing the query name

我的意图是让用户 select 列表框中的一个或多个查询名称,并提示使用这些名称执行查询。

到目前为止,我有这个代码:

Private Sub Command43_Click()
  Dim rs As DAO.Recordset
  Dim valSelect As Variant
  Dim strValue As String

    For Each valSelect In Me.Combo29.ItemsSelected
    strValue = strValue & "'" & Me.Combo29.ItemData(valSelect) & "',"
    strValue = Left(strValue, Len(strValue) - 1)

    Set rs = CurrentDb.OpenRecordset(strValue)

    Debug.Print rs
   rs.Close
 Set rs = Nothing
 Next valSelect
MsgBox "Complete!"
End Sub

当 运行 代码时,出现 Access 找不到查询名称的错误。

请帮忙!

一个命令只能打开一个查询,所以试试:

For Each valSelect In Me!Combo29.ItemsSelected
    strValue = Me!Combo29.ItemData(valSelect)
    Set rs = CurrentDb.OpenRecordset(strValue)
    Debug.Print strValue, rs.RecordCount
Next
rs.Close
Set rs = Nothing

并将您的控件重命名为有意义的名称。