我有一个MultiSelect列表框,我试图将其用作查询中的条件。我很好,直到代码行“qdf.SQL = strSQL”,此时我收到错误代码3131.如果我在“strSQL =”命令之后放置一个消息框,它似乎填充了正确的数据。
我从表单上的类别列表中提取(表单(“frmMain”)。listCat1)。
我做错了什么?先感谢您。
Private Sub CAT1_Criteria()
Dim varItem As Variant
Dim strCAT1 As String
Dim ctl As Control
Dim strSQL As String
Dim db As Database
Dim qdf As QueryDef
Set ctl = Forms("frmMain").listCat1
For Each varItem In ctl.ItemsSelected
strCAT1 = strCAT1 & ",'" & ctl.ItemData(varItem) _
& "'"
Next varItem
If Len(strCAT1) = 0 Then
strCAT1 = "Like '*'"
Else
strCAT1 = Right(strCAT1, Len(strCAT1) - 1)
strCAT1 = "IN(" & strCAT1 & ")"
End If
strSQL = "SELECT dbo_CATEGORY1.* FROM dbo_CATEGORY1" & _
"WHERE dbo_CATEGORY1.[LEVEL1] " & strCAT1 & ";"
Set db = CurrentDb
Set qdf = db.QueryDefs("qryCAT1_Sel")
qdf.SQL = strSQL
'Set qdf = Nothing
'Set db = Nothing
DoCmd.OpenQuery "qryCAT1_Sel"
End Sub