MS Access:使用VBA通过多选列表框更改查询

时间:2018-10-15 15:41:34

标签: database vba ms-access

我正在尝试执行一个基于多选列表框的选择而更改的查询。

我的代码可以很好地用于1个项目,但是如果您尝试选择多个项目,则无法正常工作。

Private Sub Toggle7_Click()

Dim strSQL As String
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Set db = Application.CurrentDb
Set qdf = db.QueryDefs("SSSBasic")
Dim Criteria1 As String

Dim strList As String
Dim FstrList As String
Dim varSelected As Variant

'Criteria1 = Me.List_Group

Dim ctl As Control

Set ctl = Me!List_Group
  If ctl.ItemsSelected.Count = 0 Then
    MsgBox "You haven't selected anything"
  Else
    For Each varSelected In ctl.ItemsSelected
      strList = strList & ctl.ItemData(varSelected) & " Or "
    Next varSelected
    FstrList = Left$(strList, Len(strList) - 4)
    'MsgBox "You selected the following items:" & vbCrLf & FstrList
  End If


strSQL = "SELECT * FROM SSSTable WHERE (SSSTable.Group = """ & FstrList & """)"

qdf.SQL = strSQL

DoCmd.OpenQuery "SSSBasic"

End Sub

当我从列表框中选择“过程”和“非过程”时,代码将运行,但是记录为零。这就是SQL的样子:

SELECT *
FROM SSSTable
WHERE (((SSSTable.Group)="Proc Or Non-Proc"));

我不知道如何使它工作...

谢谢您的帮助!

0 个答案:

没有答案