philippe_b said:
I've just tried that solution as well but I can't make it work !
Looks like using a multiple selection listbox as a search criteria is
harder than it appears !
It sounds like you have a table (say "tblContacts")
with a field (say "PostCode") and first 2 chars of
[PostCode] are meaningful for a criteria/search query.
Helen Feddema wrote an excellent article for
WoodysAccessWatch on Paired MultiSelect Listboxes.
Windows and Access use them alot and they are "kind" to the user.
The listbox on the left presents "Available" items (like first 2 chars of PostCode).
The listbox on the right shows "Selected" items.
The user selects one or more items in a listbox, then clicks on "arrow"
command buttons between the lists to move from Available to Selected
(or vica versa)
Search for Vol 2, No.22 on the following site:
http://www.woodyswatch.com/access/archives.asp
or "Access Archon Column #72"
Behind the scenes you have two tables (tblAvailableItems and tblSelectedItems,
each with a single field "PCode").
The two listboxes (lstAvailableItems and lstSelectedItems) are bound to these two tables.
When you load your "Select" form that the two listboxes are on,
you empty the the two tables and then assign "available field" to tblAvailableItems.
Using what assumed above, here be *untested* sample code:
'********* start form load code ********************
Private Sub Form_Load()
On Error GoTo ErrorHandler
Dim dbs as DAO.Database
Set dbs = CurrentDb()
'Clear tables of available and selected items
dbs.Execute "DELETE * FROM tblSelectedItems", dbFailOnError
dbs.Execute "DELETE * FROM tblAvailableItems", dbFailOnError
'Fill table of available items from your table
dbs.execute "INSERT INTO tblAvailableItems " _
& "SELECT DISTINCT Left([PostCode],2) FROM tblContacts " _
& "WHERE Len([PostCode]) > 1;", dbFailOnError
dbs.Close
Me![lstAvailableItems].Requery
Me![lstSelectedItems].Requery
ErrorHandlerExit:
Set dbs = Nothing
Exit Sub
ErrorHandler:
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End Sub
'********* end load form code ***************************
'The "arrows" (cmdAdd and cmdRemove) delete user-selected value(s)
from one table and put them in the other.
'******* start of Add/Remove code ***************
Private Sub cmdAdd_Click()
On Error GoTo ErrorHandler
Dim dbs as DAO.Database
Dim varItem As Variant
Dim rstSelected as DAO.Recordset
Dim rstAvailable as DAO.Recordset
Dim lstAvailable As Access.ListBox
Dim lstSelected As Access.ListBox
Set lstSelected = Me![lstSelectedItems]
Set lstAvailable = Me![lstAvailableItems]
'Check that at least one item has been selected
If lstAvailable.ItemsSelected.Count = 0 Then
MsgBox "Please select at least one item"
lstAvailable.SetFocus
Exit Sub
End If
Set dbs = CurrentDb
Set rstSelected = dbs.OpenRecordset("tblSelectedItems", dbOpenDynaset)
Set rstAvailable = dbs.OpenRecordset("tblAvailableItems", dbOpenDynaset)
'Iterate through collection of items selected in the listbox
For Each varItem In lstAvailable.ItemsSelected
strItem = lstAvailable.ItemData(varItem)
'Debug.Print "Selected item: " & strItem
'Append selected item to Selected Items list
With rstSelected
strSearch = "[PCode] = " & Chr$(39) & strItem & Chr$(39)
.FindFirst strSearch
If .NoMatch = True Then
.AddNew
![PCode] = strItem
.Update
End If
End With
'Delete selected item from Available Items list
With rstAvailable
strSearch = "[PCode] = " & Chr$(39) & strItem & Chr$(39)
.FindFirst strSearch
If .NoMatch = False Then
.Delete
End If
End With
Next varItem
rstSelected.Close
rstAvailable.Close
lstAvailable.Requery
lstSelected.Requery
ErrorHandlerExit:
Set rstSelected = Nothing
Set rstAvailable = Nothing
Set dbs = Nothing
Exit Sub
ErrorHandler:
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End Sub
Private Sub cmdRemove_Click()
On Error GoTo ErrorHandler
Dim dbs as DAO.Database
Dim varItem As Variant
Dim rstSelected as DAO.Recordset
Dim rstAvailable as DAO.Recordset
Dim lstAvailable As Access.ListBox
Dim lstSelected As Access.ListBox
Set lstSelected = Me![lstSelectedItems]
Set lstAvailable = Me![lstAvailableItems]
'Check that at least one item has been selected
If lstSelected.ItemsSelected.Count = 0 Then
MsgBox "Please select at least one item"
lstSelected.SetFocus
Exit Sub
End If
Set dbs = CurrentDb
Set rstSelected = dbs.OpenRecordset("tblSelectedItems", dbOpenDynaset)
Set rstAvailable = dbs.OpenRecordset("tblAvailableItems", dbOpenDynaset)
'Iterate through collection of items selected in the listbox
For Each varItem In lstSelected.ItemsSelected
strItem = lstSelected.ItemData(varItem)
Debug.Print "Selected item: " & strItem
'Append selected item to Available Items list
With rstAvailable
strSearch = "[PCode] = " & Chr$(39) & strItem & Chr$(39)
.FindFirst strSearch
If .NoMatch = True Then
.AddNew
![PCode] = strItem
.Update
End If
End With
'Delete selected item from Selected Items list
With rstSelected
strSearch = "[PCode] = " & Chr$(39) & strItem & Chr$(39)
.FindFirst strSearch
If .NoMatch = False Then
.Delete
End If
End With
Next varItem
rstSelected.Close
rstAvailable.Close
lstAvailable.Requery
lstSelected.Requery
ErrorHandlerExit:
Set rstSelected = Nothing
Set rstAvailable = Nothing
Set dbs = Nothing
Exit Sub
ErrorHandler:
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End Sub
'********** end of add/remove code **************
So.....
in your "filter" query, add "tblSelected,"
join tblContacts.PostCode to tblSelected.PCode,
then go into SQL View and change join from
ON
tblContacts.PostCode = tblSelected.PCode
to
ON
Left(tblContacts.PostCode,2) = tblSelected.PCode