Hi all, I have a piece of code for make the listbox multiple selection and it keep give me errors, can anyone take a look at my code and fix for me?
Code:
Private Sub Slc_Sub_AfterUpdate()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rs As DAO.Recordset
Dim strWhere As String
Dim ctl As Control
Dim varItem As Variant
Set db = CurrentDb()
Set qdf = db.QueryDefs("(Result) Selection")
Set rs = db.OpenRecordset("Select * from [SubArea]", dbOpenSnapshot)
If Me.Slc_Sub.ItemsSelected.Count = 0 Then
MsgBox "Must select at least 1 subArea"
Exit Sub
End If
Set ctl = Me.Slc_Sub
For Each varItem In ctl.ItemsSelected
strWhere = strWhere & "'" & ctl.ItemData(varItem) & "',"
Next varItem
strWhere = Left(strWhere, Len(strWhere) - 1)
strSql = "SELECT " & rs.Fields(1).Value & " FROM [SubArea] WHERE (" & rs.Fields(1).Value & " IN (" & strWhere & "));"
qdf.SQL = strSql
End Sub
The [SubArea] is a query contain only one column of information which selected from a option group, so it is changed depending on the different choice button clicked.
The [(Result) Selection] is a query that I want to pass the multiple selection value from the Listbox to this query
Slc_Sub is my ListBox