PDA

View Full Version : Solved: Query by Year and other Criteria



JustJerry
02-03-2006, 11:44 AM
I am trying to build a query based on a specific Year and one other field.

I have this query which will show records based on a specific month, AND a specific TruckID or ALL TruckID's should this be left Null. This works great:

SELECT tblFinalMile.EventDate, tblFinalMile.TruckID, tblFinalMile.StatePr, tblFinalMile.Elapsed
FROM tblFinalMile
WHERE (((tblFinalMile.TruckID)=[Forms]![frmStart]![cboTruckID]) AND
((Format([EventDate],"mmm yyyy"))=Format([Forms]![frmStart]![SearchDate],"mmm yyyy"))) OR
(((Format([EventDate],"mmm yyyy"))=Format([Forms]![frmStart]![SearchDate],"mmm yyyy")) AND
(([Forms]![frmStart]![cboTruckID]) Is Null))
ORDER BY tblFinalMile.TruckID, tblFinalMile.StatePr;

Now I am trying to create a second query to basically do the same thing, except to show records for a specific year and have so far come up with this:

SELECT tblFinalMile.EventDate, tblFinalMile.TruckID, tblFinalMile.StatePr, tblFinalMile.Elapsed
FROM tblFinalMile
WHERE ((DatePart("yyyy",[EventDate])=DatePart("yyyy",[Forms]![frmStart]![SearchDate])));

If I enter in the same criteria for cboTruckID as I did in the above query for the month, it keeps showing me ALL records no matter if cboTruckID is Null or set to a specific ID and ignores the criteria for the 'Year'.

I've tried several combinations, but they when running the query, I get NO records to show.

Any Help is much appreciated.

JustJerry
02-03-2006, 12:06 PM
From another suggestion, I got this code to work:

SELECT tblFinalMile.EventDate, tblFinalMile.TruckID, tblFinalMile.StatePr, tblFinalMile.Elapsed
FROM tblFinalMile
WHERE (((tblFinalMile.TruckID)=[Forms]![frmStart]![cboTruckID]) AND_
(DatePart("yyyy", [EventDate])=DatePart("yyyy",[Forms]![frmStart]![SearchDate]))_
OR (([Forms]![frmStart]![cboTruckID]) Is Null) AND
(DatePart("yyyy", [EventDate])=DatePart("yyyy",[Forms]![frmStart]![SearchDate])));

Norie
02-04-2006, 07:48 AM
Why not just use the Year function?

JustJerry
02-06-2006, 08:57 AM
Well, Norie, funny thing is I just learned about the 'Year' function about the time I was shutting down for the day last Friday. Just when I get ONE function down, I learn something new and easier! :yes