I have the below code added to a form in my db which is working fine (for now :) ).
SQL = "SELECT top 1 tblDataEntry.[URN], " & _
"tblDataEntry.[Receipt Date], tblDataEntry.[Supplier Name], tblDataEntry.[Lock], " & _
"tblDataEntry.[Document], tblDataEntry.[Order Type], " & _
"tblDataEntry.[PO #], tblDataEntry.[Invoice Value], tblDataEntry.[Entrytime], " & _
"tblDataEntry.[Comment] " & _
"FROM tblDataEntry " & _
"WHERE (((tblDataEntry.[URN]) Is Not Null) " & _
"AND ((tblDataEntry.[Order Type]) ='PO' ) " & _
"AND ((tblDataEntry.[Lock])= False))"
I want to add a line to this code so I only find records that are more than 24 hours old from the time of running and keep getting stuck.
This is what i was trying:
"AND ((tblDataEntry.[Entrytime]) < Dateadd(dd,-1,Now()))"
Note: Entrytime is a date/time field in the table which has already been filled with the NOW function on a previous form..
I've tried all sorts of combination to add a line in to do this but nothing works.
My guess is problem in parentheses
Try Like this
SELECT TOP 1
T.[URN],
T.[Receipt Date],
T.[Supplier Name],
T.[Lock],
T.[Document],
T.[Order Type],
T.[PO #],
T.[Invoice Value],
T.[Entrytime],
T.[Comment]
FROM tblDataEntry AS T
WHERE T.[URN] Is Not Null AND
T.[Order Type] ='PO' AND
T.[Lock] = False AND
T.[Entrytime] < DateAdd(dd,-1,Now())
EDIT
SQL="SELECT TOP 1 T.[URN],T.[Receipt Date],T.[Supplier Name],T.[Lock],T.[Document], T.[Order Type],T.[PO #], T.[Invoice Value], T.[Entrytime], T.[Comment] FROM tblDataEntry AS T WHERE T.[URN] Is Not Null AND T.[Order Type] ='PO' AND T.[Lock] = False AND T.[Entrytime] < DateAdd(dd,-1,Now())"
Hope this help you
In Access, DateAdd
requires a string value as its first option. Change yours to this ...
Dateadd("d", -1, Now())
However I'm uncertain you really want to subtract 1 "d" for the comparison. If you want 24 hours instead, use this ...
Dateadd("h", -24, Now())
Actually, I would be tempted to handle both date values as double-precision float numbers (which they are "under the hood") and do the comparison this way ...
CDbl(de.[Entrytime]) < (CDbl(Now()) -1)
Try a simple query in the Access query designer.
SELECT de.*
FROM tblDataEntry AS de
WHERE
de.[URN] Is Not Null
AND de.[Order Type] ='PO'
AND de.[Lock]= False
AND CDbl(de.[Entrytime]) < (CDbl(Now()) -1)
Beware CDbl
will throw an error with Null for [Entrytime]
. You could avoid that problem by removing CDbl
AND de.[Entrytime] < (Now() -1)
In that case, the Date/Time values will still be evaluated as double-precision floats, but without triggering an error when [Entrytime]
is Null.
Adjust as needed, and after you get that part working, add your field selections back again. And, since you want SELECT TOP 1
, include an ORDER BY
clause.
Finally, after you have the query you want, adapt your VBA to produce the same SELECT
statement. Or save the query and use that named query in your VBA code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.