1

我需要计算过去十二个月人们轮班的次数。我想用DateSerial. 如果我输入实际日期而不是DateSerial. 我通过更改DateSerial为返回十二个月前以外的日期来测试我的代码,但我的代码总是给出所有数据总和的数字 - 追溯到十二个月以上。所以它忽略了 MyDt。我哪里错了?

Public Function Shifts() As Integer

Dim MyDt As Date

MyDt = DateSerial(Year(Date), Month(Date) - 12, 1) - 1

Shifts = DSum("Shifts", "tblWorkNew", "GP = " & GPLookup & "And Month > #31/10/2012#")  'This works for any date.
Shifts = DSum("Shifts", "tblWorkNew", "GP = " & GPLookup & "And Month > " & MyDt) 'This only gives all data, i.e. ignores MyDt

End Function
4

1 回答 1

3

MyDt是日期/时间,但您将其作为字符串包含在您的DSum条件参数中。因此,将值转换为数据库引擎识别为您想要的日期的字符串。

Shifts = DSum("Shifts", "tblWorkNew", _
    "GP = " & GPLookup & _
    " And [Month] > " & Format(MyDt, "\#yyyy-mm-dd\#"))

我之前加了一个空格,And因为看起来好像少了一个。我假设MonthtblWorkNew表中字段的名称,因此将其括在方括号中以通知数据库引擎您不想要该Month()函数。

于 2013-04-25T20:22:05.560 回答