Results 1 to 15 of 15
  1. #1
    omer123456 is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    19

    Diaply last 4 hrs of data in a Query

    Hi,



    I have a table with data in it....we collect data every hour so on any given day we can have 24 time stamps.

    Field name: Date:
    Data Type: Date/Time

    I am trying to build a Query from that table, where the "Criteria" for the "Date:" is to display only last 4 hours of data

    What should I use as my criteria

    Thanks
    Omer

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Use DateAdd()
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Join Date
    Apr 2017
    Posts
    1,792
    As start, change the field name Date to something different, as Date is reserved as the name of Date function!

    The criteria for your query must be (on fly) something like '... WHERE NOW() - YourDate < 5/12 ...'
    (4/12 does match for 4 hours, but the last time stamp may be up to hour earlier - so <5/12 (NB! Not <=5/12!) must always include 4 last timestamps)

  4. #4
    omer123456 is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    19
    This is giving me a syntax error DateAdd()


    Thx

  5. #5
    omer123456 is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    19
    I googled & it gave me this...... [DATE:]>=Now()-5/12, but when I enter it in the criteria field in a query it becomes [DATE:]>=Now()-#5/12/2025#

    Somehow its taking 5/12....last 5 hrs out of 12 and converting it to a date

    Thanks

  6. #6
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    565
    SELECT Orders.OrderDate
    FROM Orders
    WHERE Orders.OrderDate > DateAdd("h",-4,Now())


  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by omer123456 View Post
    This is giving me a syntax error DateAdd()


    Thx
    You cannot even be bothered to lookup the syntax?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Try:

    SELECT TOP 4 [ID], tablename.* FROM tablename ORDER BY [Date] DESC;
    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.

  9. #9
    omer123456 is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    19
    Thanks for your smart comment. If I knew what I was looking for, I wouldn't have asked.

    Did you test it before posting?

    Thanks anyway

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by omer123456 View Post
    Thanks for your smart comment. If I knew what I was looking for, I wouldn't have asked.

    Did you test it before posting?

    Thanks anyway
    It is a function.? You need to give it your specific parameters so it returns what you want?
    Did you think it was miraculously going to know what those were?
    @June7's offering is the easiest for the last 4 entries anyway, whether they are within the last 4 hours or not.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #11
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    565
    I'd be careful biting the hand that feeds you. =(

    if you use the wizard when you're doing this kind of thing, it'll help you build the expression. Go into the query in design view, then click on the Builder button then click on Functions > Built-in Functions > Date/Time > DateAdd

    and it'll show the syntax:
    DateAdd(«interval», «number», «date»)

  12. #12
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    433
    Last 4 hours of the Latest date entry, or last 4 hours of each Date entry?

  13. #13
    Join Date
    Apr 2017
    Posts
    1,792
    Quote Originally Posted by omer123456 View Post
    I googled & it gave me this...... [DATE:]>=Now()-5/12, but when I enter it in the criteria field in a query it becomes [DATE:]>=Now()-#5/12/2025#

    Somehow its taking 5/12....last 5 hrs out of 12 and converting it to a date
    It's morning for me now, so I could test it.
    Somehow I managed to use 12 hours in day instead of 24 in my late hours yesterday And probably it would be wise to use the syntax ACCESS cant be too helpfull with too. The condition below worked for me!
    Code:
    SELECT * FROM tblTest WHERE TSTime+4/24>NOW;

  14. #14
    omer123456 is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    19
    Thanks, I am not biting anyone. I appreciate anyone's help.

    I am new to this & learning; Experts need to realize that too..... Maybe it's easy for you guys, but I am new

    Thanks for everyone's help !!!!

  15. #15
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Did you see post #8?

    Welshgasman only suggesting you learn to help yourself. You were provided an intrinsic Access function to use, instead of just coming back with "I don't know what that is", make some effort to find out for yourself. Starting with Microsoft documentation. https://support.microsoft.com/en-us/...9-79b17dcd0df1
    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. Replies: 6
    Last Post: 01-20-2018, 05:01 PM
  2. Elapsed time hrs mins secs
    By dasousa in forum Programming
    Replies: 20
    Last Post: 05-17-2015, 08:39 AM
  3. Billing first 2 hrs at one rate
    By wnicole in forum Queries
    Replies: 9
    Last Post: 12-12-2013, 07:18 PM
  4. calculation hrs for schedule
    By ultra5219 in forum Access
    Replies: 6
    Last Post: 05-24-2013, 12:07 AM
  5. Report last 12 Hrs Shift Report
    By mr_bank_sa in forum Reports
    Replies: 3
    Last Post: 04-25-2011, 02:35 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