:
Hey Nick,
Thanx for your prompt response and the code however, I didn't quite get the
join section.
SELECT tbl.fld1, tbl.fld2 FROM tbl INNER JOIN tempquery1 ON tbl.fld1 =
tempquery.fld1INNER JOIN tempquery2 ON tbl.fld2 = tempquery.fld2;
is tbl.fld1 = the table name and field name of the temp query or the
original table? also, do where do I include the field that initially joins
the two fields together?
Basically, I've filtered "skills" and "months" tables using the multi-list
boxes which are linked together (using relationships) by a "ID no". I want to
join the two temp queries (created when filtering) so that only the "ID no.",
"skills" AND "months" I've filtered come up together in a 'final query'.
Can you please help?!
Thanx
Rana
:
Hi Rana,
It is actually really simple using the code below.
****Start Code****
'Create a new tempquery for each listbox
"Don't forget the declarations
strSQL = "SELECT * FROM YourTable1" 'this is the table that the listbox
is built from.
For i = 0 To List1.ListCount - 1
If List1.Selected(i) Then
strIN = strIN & "'" & List1.Column(0, i) & "',"
End If
Next i
strwhere = " WHERE Yourfield in (" & Left(strIN, Len(strIN) - 1) & ")"
strSQL = strSQL & strwhere
MyDB.QueryDefs.Delete TempQuery1
Set qdf = MyDB.CreateQueryDef(TempQuery1, strSQL)
strSQL = "SELECT * FROM YourTable2" 'this is the table that the listbox
is built from.
For i = 0 To List2.ListCount - 1
If List2.Selected(i) Then
strIN = strIN & "'" & List2.Column(0, i) & "',"
End If
Next i
strwhere = " WHERE Yourfield in (" & Left(strIN, Len(strIN) - 1) & ")"
strSQL = strSQL & strwhere
MyDB.QueryDefs.Delete TempQuery2
Set qdf = MyDB.CreateQueryDef(TempQuery2, strSQL)
**** End Code****
As long as you are creating a different tempquery for each listbox you can
use them to filter another query.
eg
SELECT tbl.fld1, tbl.fld2 FROM tbl INNER JOIN tempquery1 ON tbl.fld1 =
tempquery.fld1INNER JOIN tempquery2 ON tbl.fld2 = tempquery.fld2;
(yes I know the syntax is not right but it is a concept rather than example)
That should get you on the way.
Don't overlook the simple solutions however, sometimes it is more expedient
to filter one listbox based on the selections of another listbox on the same
form to achieve similar results and a combination of that approach and the
code above can be very powerful.
Regards,
Nick.
:
Biz Enhancer,
Thanx for the code, it was very useful. I'm going to be a slight pain but
basically I want to use two multi-list boxes and the 'AND' logical operator
to filter a query.
I've got some other code that allows me to filter using one multi-list box
but I can't seem to get two to work.
Can you please help me out?!
Rana
:
Forgot to put the declarations at the start of the code!!!
****Start of code*****
Dim MyDB As Database
Dim qdf As QueryDef
Dim i As Integer, strSQL As String
Dim strwhere As String, strIN As String
Set MyDB = CurrentDb()
strSQL = "SELECT * FROM YourTable" 'this is the table that the listbox
is built from.
For i = 0 To List1.ListCount - 1
If List1.Selected(i) Then
strIN = strIN & "'" & List1.Column(0, i) & "',"
End If
Next i
strwhere = " WHERE Yourfield in (" & Left(strIN, Len(strIN) - 1) & ")"
strSQL = strSQL & strwhere
MyDB.QueryDefs.Delete TempQuery
Set qdf = MyDB.CreateQueryDef(TempQuery, strSQL)
*****End of Code*****
:
Hi John,
For a multiselect listbox to work as a filter you need to create another
query based on the listbox.
Put this code for execution between the selection of the listbox and running
your main query
****Start of code*****
strSQL = "SELECT * FROM YourTable" 'this is the table that the listbox
is built from.
For i = 0 To List1.ListCount - 1
If List1.Selected(i) Then
strIN = strIN & "'" & List1.Column(0, i) & "',"
End If
Next i
strwhere = " WHERE Yourfield in (" & Left(strIN, Len(strIN) - 1) & ")"
strSQL = strSQL & strwhere
MyDB.QueryDefs.Delete TempQuery
Set qdf = MyDB.CreateQueryDef(TempQuery, strSQL)
*****End of Code*****
This will create a query named TempQuery which will contain the value
selected in the multiselect listbox. Just link this into your existing query
for it to act as the filter.
HTH
Regards,
Nick
:
Hi All,
I have been able to build a query that is filtered by a selection in a
Listbox, but now i want to use a multi select list box so that two or more
items can be included in the query. I constructed the query pointing to the
Listbox but all I get is an empty dataset. How can this be accomplished?
As always your help is appreciated.
Regards,
John DumayOkay, the way it works is this.
In the query 'tbl' is the table you wish to filter. The temp queries are
your filters. When you run the code after making the selections in the
(multiselect) list boxes, the main query, which provides your results, will
filtered based on the selection from your listboxes.
E.g.
Tbl1 fields:
ClientID
Clientname
HairColourID
ShoeSizeID
SuburbID
HairColourTbl fields:
HairColourID
HairColour
ShoeSizeTbl fields:
ShoeSizeID
Shoesize
Listbox1 Rowsource = "SELECT * FROM HairColour"
Listbox2 Rowsource = "SELECT * FROM ShoeSize"
When the code runs, it creates a tempquery from Listbox1 selections and a
tempquery from Listbox2 selections.
Tempquery1 would have:
2,Green
5,Black
7,Purple
Tempquery2 would have:
3,US12
8,US5
The query that provides for results would be:
SELECT Client.ClientID, Client.ClientName, Tempquery1.HairColour,
Tempquery2.ShoeSize FROM Client (INNER JOIN HairColour ON Client.HairColourID
= HairColour.HairColourID) INNER JOIN ShoeSize ON Client.ShoeSizeID =
ShoeSize.ShoeSizeID;
Therefore, the result will be all clientIDs, ClientNames, haircolour, and
shoesize WHERE (haircolour is Green, Black, Or Purple) And (ShoeSize is US5
Or US12).
Changing the Join Structure to LEFT or RIGHT joins will allow some looser
filtering.
Hopefully all that makes more sense.
Regards,
Nick.