Hi, I post my solution for the multiple list box pass selection to query before, however, I just found a error while test my project. If I unselect all my selection in the list box, it will show me that my strSQL statment is error, expect select, update or insert... can anyone help me to figure it out?
Code:
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim lngLen As Long
Dim strDelim As String
Dim strWhere As String
strDelim = """"
Dim strSQL As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("Selection2")
With Me.Spc_Slc
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
strWhere = strWhere & strDelim & .ItemData(varItem) & strDelim & ","
strCriteria = strCriteria & """" & .Column(1, varItem) & """, "
Else
MsgBox "please select at least one species.", vbExclamation
End If
Next varItem
End With
lngLen = Len(strWhere) - 1
If lngLen > 0 Then
strWhere = "Species IN (" & Left$(strWhere, lngLen) & ")"
lngLen = Len(strCriteria) - 2
If lngLen > 0 Then
strSQL = "SELECT Species, SpeciesRef FROM Species WHERE Species.Species IN (" & strCriteria & ");"
End If
End If
qdf.SQL = strSQL <==debug error here