Results 1 to 4 of 4
  1. #1
    bcarter17 is offline Advanced Beginner
    Windows 10 Access 2007
    Join Date
    Jun 2021
    Location
    Richmond MO
    Posts
    46

    Form Function Problem

    Hello all,
    I am working on an Access 2007 form and I have a need to create a calculated field, that will display the number of trucks on a job that have been actively picking up loads within the last 2 hours. Here is my setup.



    The data source is a query named qryMichelsDeliveryOrder
    The field I want to count with criteria is [Contractor]
    The criteria would be using a field named [Last Loaded], then using current time, count the Contractor criteria only if the [Last Loaded] field is within the last 2 hours.

    I am trying to using DCount as a function in the form field using the code below.

    =DCount('[Contractor]','qryMichelsDeliveryOrder","[Last Loaded]>=DateAdd("h",-2,Now())")

    However, I am getting a string error each time I try to insert it. Any thoughts on what I am missing?

  2. #2
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,421
    Could be your mix of single and double quotes. Could also be that [last loaded] contains the date, in which case you would not be able to compare dates vs time. Could also be that you're trying to compare an hour value to a time value (2 vs 5:00 PM) but using Now will cause your expression to include the date. Seeing some data might help if modifying the quote marks doesn't fix it. I also think you'll need date delimiters. Try

    =DCount("[Contractor]","qryMichelsDeliveryOrder","[Last Loaded]>=#" & DateAdd("h",-2,Now()) & "#")

    Knowing the text of the error message is always helpful. The error number alone is not.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    433
    you may also try:
    Code:
    =DCount("[Contractor]",'"ryMichelsDeliveryOrder","Hour([Last Loaded])>=Hour(Now())-2")

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    jojo, showing a stray solo apostrophe in that DCount expression. Expression probably needs to consider year and month in order to limit to "last 2 hours" as Madpiet shows, although concatenation and # should not be necessary.

    Reverse apostrophes and quotes. This works for me:

    DCount("*","qryMichelsDeliveryOrder","[Last Loaded]>=DateAdd('h',-2,Now())")
    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. Public Function problem on form open
    By d9pierce1 in forum Forms
    Replies: 1
    Last Post: 11-27-2018, 11:19 PM
  2. Function in a Function
    By BLFOSTER in forum Programming
    Replies: 1
    Last Post: 01-29-2016, 01:20 PM
  3. Replies: 8
    Last Post: 11-04-2014, 10:44 PM
  4. Replies: 8
    Last Post: 01-31-2014, 01:45 PM
  5. Want function to get current function name
    By Davis DeBard in forum Programming
    Replies: 2
    Last Post: 08-13-2009, 05:02 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