I have a multi select list box called "Spc_Slc" and want to pass the selected value as criteria to the field called "Species" in my query. I don't know how to connect the code to my query without click command button, because I only want to use the query as data source to my other form. I have the code like this:
Private Sub Spc_Slc_AfterUpdate()
On Error GoTo Err_Run
If Spc_Slc.ItemsSelected.Count = 0 Then
MsgBox "No items selected.", vbExclamation
Exit Sub
End If
Dim strCriteria As String
Dim varItem As Variant
' Build a criteria string
For Each varItem In Spc_Slc.ItemsSelected
strCriteria = strCriteria & "', '" & Spc_Slc.ItemData(varItem)
Next varItem
strCriteria = Right(strCriteria, Len(strCriteria) - 3) & "'"
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Set rs = dbs.OpenRecordset("SELECT [Species].[SpeciesRef], [Species].[Species] FROM [Species] ORDER BY [Species]" & _
"WHERE [Species].[Species] IN (" & strCriteria & ");")
rs.Close
Exit_Run:
Set rs = Nothing
Exit Sub
Err_Run:
MsgBox Err.Description
Resume Exit_Run
End Sub
Thanks!
Private Sub Spc_Slc_AfterUpdate()
On Error GoTo Err_Run
If Spc_Slc.ItemsSelected.Count = 0 Then
MsgBox "No items selected.", vbExclamation
Exit Sub
End If
Dim strCriteria As String
Dim varItem As Variant
' Build a criteria string
For Each varItem In Spc_Slc.ItemsSelected
strCriteria = strCriteria & "', '" & Spc_Slc.ItemData(varItem)
Next varItem
strCriteria = Right(strCriteria, Len(strCriteria) - 3) & "'"
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Set rs = dbs.OpenRecordset("SELECT [Species].[SpeciesRef], [Species].[Species] FROM [Species] ORDER BY [Species]" & _
"WHERE [Species].[Species] IN (" & strCriteria & ");")
rs.Close
Exit_Run:
Set rs = Nothing
Exit Sub
Err_Run:
MsgBox Err.Description
Resume Exit_Run
End Sub
Thanks!