多选列表框查询

时间:2016-10-20 22:27:26

标签: ms-access access-vba access

我有一个表单,我想通过使用mulitSelect列表框来运行查询。我想我可能会有它,但SQL部分是给我带来问题的。 表是" SupervisorsQuery" 查询是" Query1" 列表框是" lstUser" 查询通过按钮的单击事件运行。错误看起来从FROM语句开始。任何帮助都会很棒!!

Private Sub Command6_Click()
Dim Q As QueryDef, DB As Database
Dim Criteria As String
Dim ctl As Control
Dim itm As Variant

Set ctl = Me![lstUser]
For Each itm In ctl.ItemsSelected
    If Len(Criteria) = 0 Then
        Criteria = Chr(34) & ctl.ItemData(itm) & Chr(34)
    Else
        Criteria = Criteria & "," & Chr(34) & ctl.ItemData(itm) & Chr(34)
    End If
Next itm
If Len(Criteria) = 0 Then
    itm = MsgBox("You must select one or more items in the list box")
    Exit Sub
End If

Set DB = CurrentDb()
Set Q = DB.QueryDefs("Query1")
Q.SQL = "Select SupervisorsQuery.Date, SupervisorsQuery.Client,SupervisorsQuery.[Job Code Description], SupervisorsQuery.[Perf Percent]"
FROM SupervisorsQuery
WHERE ((SupervisorsQuery.[User ID]) In (" & Criteria & "));"

Q.Close
DoCmd.OpenQuery "Query1"


End Sub

1 个答案:

答案 0 :(得分:0)

使用此:

Q.SQL = "Select SupervisorsQuery.[Date], SupervisorsQuery.Client,SupervisorsQuery.[Job Code Description], SupervisorsQuery.[Perf Percent] " & _
"FROM SupervisorsQuery " & _
"WHERE ((SupervisorsQuery.[User ID]) In (" & Criteria & "));"