Results 1 to 7 of 7
  1. #1
    Matricus is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2018
    Posts
    3

    Return data with multi-date criteria in different tables

    Hey fellow data guys.



    A specific issue is bothering me a longer time and I was not able to solve it on my own.

    I have two tables (tbl1 and tbl2) and want to create a new one.

    tbl1 contains the numeric field "aID" and the short date "DateFull". tbl1 contains different data whenever the query is run as it is an import, but its structure remains.
    Except for "cID" the other fields are not unique.

    cID eID aID DateFull
    1 2345 10789 20.11.2018
    2 2346 10534 20.11.2018
    3 2345 10789 03.04.2018


    tbl2 contains additional information for the "aID" value of tbl1.

    ID aID DateStart DateEnd Role
    1 10789 01.01.2018 31.05.2018 Role 1
    2 10789 01.06.2018 Role 2
    3 10534 01.01.2018 Role 1

    ID is the only unique field.

    I want to create a new table with cID, DateFull of tbl1 and ID, Role of tbl2.

    As you can see above, tbl2 contains duplicate aID values.
    Another tricky thing is, that DateEnd can be empty. Empty equals current date (date when query runs).

    To get the fitting entry I need to check if tbl1.DateFull is between tbl2.DateStart and tbl2.DateEnd and then return the data I need.

    The final table should look like this:

    cID DateFull ID Role
    1 20.11.2018 2 Role 2
    3 03.04.2018 1 Role 1
    2 20.11.2018 3 Role 1


    In the past I did work around it by manually editing the raw data in the database to work around the DateStart/DateEnd problem and simply changing archived data.

    Hope you guys can help.

    Cheers
    Last edited by Matricus; 11-23-2018 at 04:01 AM. Reason: solved

  2. #2
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Make a query with both tables and link using the common field aID.
    Add all fields you want to the query.
    For the DateFull field set filter criteria between DateStart and Nz(DateEnd, Date).
    The Nz part should handle null values for DateEnd
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  3. #3
    kevinjoseph is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2018
    Posts
    3
    That's Use only one select query in SQL.

  4. #4
    Matricus is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2018
    Posts
    3
    Hmm... correctly applied?

    Code:
    SELECT tbl1.cID, tbl1.DateFull, tbl2.ID, tbl2.Role
    FROM tbl1 
    INNER JOIN tbl2 ON tbl1.aID = tbl2.aID
    WHERE (((tbl1.DateFull)>=[DateStart] And (tbl1.DateFull)<=Nz([DateEnd],Date())));
    Attached Thumbnails Attached Thumbnails Capture.JPG  

  5. #5
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Should normally be a comma not a semicolon in the Nz function but that depends on language settings.
    Your code sample shows a comma but your screenshot has a semicolon.

    Another way of writing it is
    Code:
    Between [DateStart] And Nz([DateEnd],Date())
    but yours should also be fine
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  6. #6
    Matricus is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2018
    Posts
    3
    Well yeah, company system... Windows is US and primary language setting is UK-English; most Office apps (not all though) require me to use a semicolon for formulas but SQL view for example is comma.
    Whatever, thanks.

  7. #7
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Does that mean it is now solved?
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

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

Similar Threads

  1. Dlookup with Date criteria always return 0
    By FL0XN0X in forum Access
    Replies: 3
    Last Post: 01-30-2018, 06:14 PM
  2. Replies: 4
    Last Post: 08-01-2016, 06:41 AM
  3. Replies: 2
    Last Post: 04-02-2015, 12:45 PM
  4. How do I return a value on multi criteria?
    By smc678 in forum Access
    Replies: 5
    Last Post: 11-20-2012, 02:35 PM
  5. Replies: 1
    Last Post: 06-17-2011, 12:59 AM

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