D
Dustin
Hello - any help in this department would be greatly appreciated
"I am trying to create a query based on choices made in three
listboxes. The user needs to be able to choose ALL as well as multiple
choices from each listbox. I have used the code below successfully for
one listbox, but am not knowledgeable enough to bring in the other two
listboxes."
Private Sub cmdOpenQuery_Click()
On Error GoTo Err_cmdOpenQuery_Click
Dim MyDB As DAO.Database
Dim qdef As DAO.QueryDef
Dim i As Integer
Dim strSQL As String
Dim strWhere As String
Dim strIN As String
Dim flgSelectAll As Boolean
Dim varItem As Variant
Set MyDB = CurrentDb()
strSQL = "SELECT * FROM Prospects"
'Build the IN string by looping through the listbox
For i = 0 To LstCity.ListCount - 1
If LstCity.Selected(i) Then
If LstCity.Column(0, i) = "All" Then
flgSelectAll = True
End If
strIN = strIN & "'" & LstCity.Column(0, i) & "',"
End If
Next i
'Test
'Create the WHERE string, and strip off the last comma of the IN
string
strWhere = " WHERE [City] in (" & Left(strIN, Len(strIN) - 1) & ")"
'If "All" was selected in the listbox, don't add the WHERE
condition
If Not flgSelectAll Then
strSQL = strSQL & strWhere
End If
MyDB.QueryDefs.Delete "FilteredProspects"
Set qdef = MyDB.CreateQueryDef("FilteredProspects", strSQL)
'Open the query, built using the IN clause to set the criteria
DoCmd.OpenQuery "FilteredProspects", acViewNormal
'Clear listbox selection after running query
For Each varItem In Me.LstCity.ItemsSelected
Me.LstCity.Selected(varItem) = False
Next varItem
Exit_cmdOpenQuery_Click:
Exit Sub
Err_cmdOpenQuery_Click:
If Err.Number = 5 Then
MsgBox "You must make a selection(s) from the list", ,
"Selection Required !"
Resume Exit_cmdOpenQuery_Click
Else
'Write out the error and exit the sub
MsgBox Err.Description
Resume Exit_cmdOpenQuery_Click
End If
End Sub
"I am trying to create a query based on choices made in three
listboxes. The user needs to be able to choose ALL as well as multiple
choices from each listbox. I have used the code below successfully for
one listbox, but am not knowledgeable enough to bring in the other two
listboxes."
Private Sub cmdOpenQuery_Click()
On Error GoTo Err_cmdOpenQuery_Click
Dim MyDB As DAO.Database
Dim qdef As DAO.QueryDef
Dim i As Integer
Dim strSQL As String
Dim strWhere As String
Dim strIN As String
Dim flgSelectAll As Boolean
Dim varItem As Variant
Set MyDB = CurrentDb()
strSQL = "SELECT * FROM Prospects"
'Build the IN string by looping through the listbox
For i = 0 To LstCity.ListCount - 1
If LstCity.Selected(i) Then
If LstCity.Column(0, i) = "All" Then
flgSelectAll = True
End If
strIN = strIN & "'" & LstCity.Column(0, i) & "',"
End If
Next i
'Test
'Create the WHERE string, and strip off the last comma of the IN
string
strWhere = " WHERE [City] in (" & Left(strIN, Len(strIN) - 1) & ")"
'If "All" was selected in the listbox, don't add the WHERE
condition
If Not flgSelectAll Then
strSQL = strSQL & strWhere
End If
MyDB.QueryDefs.Delete "FilteredProspects"
Set qdef = MyDB.CreateQueryDef("FilteredProspects", strSQL)
'Open the query, built using the IN clause to set the criteria
DoCmd.OpenQuery "FilteredProspects", acViewNormal
'Clear listbox selection after running query
For Each varItem In Me.LstCity.ItemsSelected
Me.LstCity.Selected(varItem) = False
Next varItem
Exit_cmdOpenQuery_Click:
Exit Sub
Err_cmdOpenQuery_Click:
If Err.Number = 5 Then
MsgBox "You must make a selection(s) from the list", ,
"Selection Required !"
Resume Exit_cmdOpenQuery_Click
Else
'Write out the error and exit the sub
MsgBox Err.Description
Resume Exit_cmdOpenQuery_Click
End If
End Sub