Consulting

Results 1 to 4 of 4

Thread: Solved: Query by Year and other Criteria

  1. #1

    Solved: Query by Year and other Criteria

    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:

    [VBA]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;[/VBA]

    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:

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

    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.
    Last edited by geekgirlau; 02-08-2006 at 02:23 AM. Reason: Put line breaks in code

  2. #2
    From another suggestion, I got this code to work:

    [VBA]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])));[/VBA]
    Last edited by geekgirlau; 02-08-2006 at 02:25 AM. Reason: Put line breaks in code

  3. #3
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Why not just use the Year function?

  4. #4
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •