Results 1 to 4 of 4
  1. #1
    Retroboro is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2018
    Posts
    2

    Find Last Date for a criteria in Query

    Hi (first post),

    I have 2 tables, one showing sales and the other showing the exchange rate. I want to be able to identify the exchange rate when the sale was made.

    However the exchange rate table only doesn't have a record for every date:

    Table 1 (Sales)

    Date, Sales Value
    01/11/2018, £10
    02/11/2018, £20
    03/11/2018, £10
    04/11/2018, £30

    Table 2 (Exchange Rate)
    Date, Ex Rate
    01/11/2018, 1.14
    03/11/2018, 1.16
    04/11/2018, 1.17

    The result I'm looking for is:

    Date, Sales Value, Ex Rate


    01/11/2018, £10, 1.14
    02/11/2018, £20, 1.14
    03/11/2018, £10, 1.16
    04/11/2018, £30, 1.17

    Any help would be much appreciated. Thanks

  2. #2
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    Put the exchange table rate in your query and in the criteria put <= [SalesTable].[SalesDate]

    Please rename the Date fields. Date is a function and reserved word in Access.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    Retroboro is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2018
    Posts
    2
    Hi thanks, but I'm struggling to get it to work. I've simplified the field names, hence "Date". but when I run the SQL

    SELECT DISTINCT Sales Data.Date, Sales Data.[Sales Value], Exchange Rate.Ex Rate
    FROM Sales Data LEFT JOIN Exchange Rate ON Sales Data.Date = Exchange Rate. Ex Date
    WHERE (((Exchange Rate.Ex Rate)<=[Sales Data]![Date]));

    the result I get is:

    01/11/2018, £10, 1.14
    03/11/2018, £10, 1.16
    04/11/2018, £30, 1.17

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Should also avoid spaces in field names. If used must enclose in [ ]. If you use the query designer, Access should provide the [ ]. I am surprised you get anything with the posted structure.

    One solution involves domain aggregate function:

    SELECT Sales.Date, Sales.[Sales Value], DMax("[Ex Rate]","Exchange Rate","[Date]<=#" & [Date] & "#") AS ER FROM Sales;

    Note the lack of [ ] around the table name. This is a weird exception to the requirement for [ ].

    Assumes there is only 1 rate for each date.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Query to find max date IF criteria is met
    By macbooknick in forum Queries
    Replies: 5
    Last Post: 05-05-2015, 04:51 AM
  2. Replies: 2
    Last Post: 04-02-2015, 12:45 PM
  3. Query to find last date
    By DHIGHTO in forum Access
    Replies: 1
    Last Post: 01-16-2015, 09:53 AM
  4. Replies: 2
    Last Post: 10-20-2014, 03:09 PM
  5. Replies: 3
    Last Post: 08-21-2012, 03:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums