【问题标题】:ACCESS VBA selecting multiple values from listbox and executing the query nameACCESS VBA 从列表框中选择多个值并执行查询名称
【发布时间】:2015-10-11 21:38:18
【问题描述】:

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

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

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 找不到查询名称的错误。

请帮忙!

【问题讨论】:

    标签: ms-access listbox vba recordset


    【解决方案1】:

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

    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
    

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

    【讨论】:

    • 我在 Set rs = CurrentDb.OpenRecordset(strValue) 收到一个错误,指出这是一个无效操作。
    • 上面的代码只是带有循环的sn-p。你还是得把 rs 调暗。
    • rs 变暗,但出现相同的错误: 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 = Me. Combo29.ItemData(valSelect) Set rs = CurrentDb.OpenRecordset(strValue) Debug.Print strValue, rs.RecordCount Next rs.Close Set rs = Nothing MsgBox "完成!"结束子
    • 而valSelect是一个数字而strValue是一个字符串?
    • valSelect 是 Variant 而 strValue 是一个字符串。但是,这是对我的代码的更新,我管理它做我需要的(从列表框中运行选定的查询名称(无论是单选还是多选)- Private Sub Command43_Click() Dim rs As DAO.Recordset Dim valSelect As Variant Dim strValue As String Set db = CurrentDb() For Each valSelect In Me.Combo29.ItemsSelected DoCmd.SetWarnings (Warningsoff) strValue = Me.Combo29.ItemData(valSelect) DoCmd.OpenQuery (strValue) Next DoCmd.SetWarnings (Warningson) MsgBox "Complete!"结束子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    • 2014-03-20
    相关资源
    最近更新 更多