Skip to content
Advertisement

How to make a Query in MS-Access that returns the intersection of three other queries?

I have a “search” form that allows the user to input location, date, or ID. This form generates three queries, which return appropriate results, or all records if no search term was input for that category. I intended to make a fourth query that took the intersection of the first three, but have been unable to make it work. I’ve reviewed similar questions about using the SQL INNER JOIN function but can’t make it work. SQL from each of my three queries is below. (or, if there’s a better way to do this, let me know. I tried doing it all in one query and it was a mess).

Qry_ByDate:

SELECT tbl_AssyMoves.[Assy ID], tbl_AssyMoves.[From ICA], tbl_AssyMoves.[From Loc], tbl_AssyMoves.[To ICA], tbl_AssyMoves.[To Loc], tbl_AssyMoves.Date, tbl_AssyMoves.Comments
FROM tbl_AssyMoves
WHERE (((tbl_AssyMoves.Date) Between [Forms]![Form1]![StartDate] And [Forms]![Form1]![EndDate]))

Qry_ByLoc:

SELECT tbl_AssyMoves.[Assy ID], tbl_AssyMoves.[From ICA], tbl_AssyMoves.[From Loc], tbl_AssyMoves.[To ICA], tbl_AssyMoves.[To Loc], tbl_AssyMoves.Date, tbl_AssyMoves.Comments
FROM tbl_AssyMoves
WHERE (((tbl_AssyMoves.[From ICA]) Like Forms!Form1!Combo_ICA) And ((tbl_AssyMoves.[From Loc]) Like Forms!Form1!Txt_Loc)) Or (((tbl_AssyMoves.[To ICA]) Like Forms!Form1!Combo_ICA) And ((tbl_AssyMoves.[To Loc]) Like Forms!Form1!Txt_Loc))

Qry_ByID:

SELECT tbl_AssyMoves.[Assy ID], tbl_AssyMoves.[From ICA], tbl_AssyMoves.[From Loc], tbl_AssyMoves.[To ICA], tbl_AssyMoves.[To Loc], tbl_AssyMoves.Date, tbl_AssyMoves.Comments
FROM tbl_AssyMoves
WHERE (((tbl_AssyMoves.[Assy ID]) Like [Forms]![Form1]![txt_AssyID]))

Advertisement

Answer

The intersection of the 3 queries returns all the rows for which apply all the conditions in the WHERE clauses:

SELECT tbl_AssyMoves.[Assy ID], tbl_AssyMoves.[From ICA], tbl_AssyMoves.[From Loc],
tbl_AssyMoves.[To ICA], tbl_AssyMoves.[To Loc], tbl_AssyMoves.Date, tbl_AssyMoves.Comments
FROM tbl_AssyMoves 
WHERE
  (tbl_AssyMoves.Date Between [Forms]![Form1]![StartDate] And [Forms]![Form1]![EndDate])
  And
  (tbl_AssyMoves.[Assy ID]) Like [Forms]![Form1]![txt_AssyID])
  And
  (((tbl_AssyMoves.[From ICA]) Like Forms!Form1!Combo_ICA) And ((tbl_AssyMoves.[From Loc]) Like Forms!Form1!Txt_Loc)) Or (((tbl_AssyMoves.[To ICA]) Like Forms!Form1!Combo_ICA) And ((tbl_AssyMoves.[To Loc]) Like Forms!Form1!Txt_Loc))
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement