从表单在表单中的两个日期之间运行查询时出错

问题描述 投票:0回答:1

我有一个名为FirstInLastOut的表单,如下图所示.Form

根据姓名或徽章编号,我想在两个日期之间进行搜索。

我在查询中使用以下条件:

>=[Forms]![FirstInLastOut]![StartDateEntry] And <=[Forms]![FirstInLastOut]![EndDateEntry]

这给了我包括其他月份的结果。请参阅下面的查询报告。

FirstinlastoutReport

因此,您可以在图像中看到日期的数量随着参数而下降,但也会有其他月份。

我怎样才能这样做它只会选择日期范围之间的日期?

SELECT FistClockInRaw.Badgenumber, FistClockInRaw.name, FistClockInRaw.lastname, FistClockInRaw.MinOfCHECKTIME, FLastClockOutRaw.MaxOfCHECKTIME, [MaxOfCHECKTIME]-[MinOfCHECKTIME] AS TotalHours, FLastClockOutRaw.QDate, FistClockInRaw.MinOfQTime, FLastClockOutRaw.MaxOfQTime, RawCompleteQuery.CHECKTIME
FROM RawCompleteQuery, FLastClockOutRaw INNER JOIN FistClockInRaw ON (FLastClockOutRaw.Badgenumber = FistClockInRaw.Badgenumber) AND (FLastClockOutRaw.name = FistClockInRaw.name) AND (FLastClockOutRaw.QDate = FistClockInRaw.QDate)
WHERE (((FistClockInRaw.name)=[Forms]![FirstInLastOut]![FirstNameEntry]) AND ((RawCompleteQuery.CHECKTIME)>=[Forms]![FirstInLastOut]![StartDateEntry] And (RawCompleteQuery.CHECKTIME)<=[Forms]![FirstInLastOut]![EndDateEntry]));

是查询

ms-access ms-access-2010 ms-access-2007 ms-access-2013 ms-access-2016
1个回答
0
投票

我假设表单字段StartDateEntryEndDateEntry绑定到date类型的字段。

我还假设您只想比较这些表单字段的日期部分。

因此,请尝试此条件以确保正确的日期解释:

WHERE FistClockInRaw.name=[Forms]![FirstInLastOut]![FirstNameEntry]
AND RawCompleteQuery.CHECKTIME >= Format([Forms]![FirstInLastOut]![StartDateEntry], "\#yyyy-mm-dd\#")
AND RawCompleteQuery.CHECKTIME <= Format([Forms]![FirstInLastOut]![EndDateEntry], "\#yyyy-mm-dd\#")

一句话:

请注意,每个日期字段/变量也始终包含时间部分!

因此,您当前将EndDateEntry<=进行比较的逻辑可能会带来麻烦,因为您只会在字段00:00:00中获得结束日期的结果,其时间值为CHECKTIME

如果任何CHECKTIME记录包含请求的结束日期和时间部分大于00:00:00,则不在结果中。

为避免这种情况,你应该使用<并添加一天:

And RawCompleteQuery.CHECKTIME < Format([Forms]![FirstInLastOut]![EndDateEntry] + 1, "\#yyyy-mm-dd\#")
© www.soinside.com 2019 - 2024. All rights reserved.