Hi All,
I am trying to use the following code to take data based on selections in a multi select list box to a query in access. I am new to VBA and can't figure out where I have gone wrong. When I make a selection it opens up dialog box asking for the [Location] field so I have to type it in again. Also, when repeating the process it brings up an error.
I am trying to use the following code to take data based on selections in a multi select list box to a query in access. I am new to VBA and can't figure out where I have gone wrong. When I make a selection it opens up dialog box asking for the [Location] field so I have to type it in again. Also, when repeating the process it brings up an error.
Code:
Private Sub Command22_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("Step 3 - Selected Final Fields")
For Each varItem In Me!List0.ItemsSelected
strCriteria = strCriteria & "," & Me!List0.ItemData(varItem) & ""
Next varItem
If Len(strCriteria) = 0 Then
MsgBox "You did not select anything.", vbExclamation, "Nothing to find!"
Exit Sub
End If
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
strSQL = _
"SELECT* FROM [Step 2 - Final Fields] " & _
"WHERE [Step 2 - Final Fields].[Location] IN(" & strCriteria & ");"
qdf.SQL = strSQL
DoCmd.OpenQuery "Step 3 - Selected Final Fields"
Set db = Nothing
Set qdf = Nothing
End Sub
Last edited by a moderator: