DSum与日期标准

时间:2013-04-25 20:07:41

标签: ms-access access-vba

我需要计算过去12个月中人们轮班工作的数量。我想使用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

1 个答案:

答案 0 :(得分:3)

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

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

我在And之前添加了一个空格,因为看起来有一个空缺。我假设MonthtblWorkNew表中字段的名称,因此将其括在方括号中以通知数据库引擎您不希望使用Month()函数。

相关问题