GPGeorge
George Hepworth
- Local time
- Yesterday, 20:08
- Joined
- Nov 25, 2004
- Messages
- 3,294
One of my favorite techniques for filtering queries that are used as recordsources for forms and rowsources for list and combo boxes involves tempvars.You've got to see this excellent video on tempvars by Daniel Pineault:
Here's an example.
Rich (BB code):
SELECT tblpublication.publicationid,
tblpublication.publicationtitle,
tblpublication.volume,
tblpublication.numberofvolumes,
tblpublication.catalognumber,
tblpublication.yearpublished,
tblpublication.pages,
tblpublication.edition,
tblpublication.printing,
tblpublication.coverphotolink,
tblpublication.comments,
tblpublication.mediaconditionid,
tblpublication.publisherid,
tblpublication.mediatypeid,
tblpublication.isbn,
tblpublication.confidencelevel,
tblpublication.interalcomments,
tblpublication.shelfid,
tblpublication.listprice
FROM tblpublication
WHERE Iif(Tempvarslong("lngpublicationid") = 0, 0, [publicationid]) )
IN (0, Tempvarslong("lngpublicationid")
ORDER BY tblpublication.publicationtitle;
The way it works is this. In a combo or list box on the form, the row source is a Union Query.
Code:
SELECT 0 AS PublicationID, " <ALL>" AS PublicationTitle
FROM tblPublication
UNION SELECT PublicationID, PublicationTitle
FROM tblPublication
ORDER BY PublicationTitle
The AfterUpdate event of the combo box sets the value of TempVar lngPublicationID to the value selected, either 0 or one of the existing Publication Primary Keys.
If the user selects 0 , " <ALL>" in the combo box, the form's recordsource query will match 0 and 0 and therefore return all of the records in the table. If any single PublicationID is selected in the Combo box, the form's recordsource is filtered to that specific PublicationID.