DSUM日期标准不起作用

时间:2017-04-20 20:45:43

标签: ms-access access-vba

我已尝试过在论坛上找到的所有建议,但我的代码变得混乱!

我正在使用Microsoft Access(Office 365)我的计算机是法语,我已将英语作为辅助语言安装。

我想在今年第二季度找到tblEtsySales的所有销售记录。我正在使用DSUM汇总TotalReceived字段中的记录,并希望按日期过滤。当它应该为零时,我会收到意外的金额。

这是我的代码:

Dim Q2 As Currency
Dim Quarter2 As String

Quarter2 = "[Sale Date] BETWEEN #01/04/2017# And #30/06/2017#"

If IsNull(Q2 = DSum("[TotalReceived]", "tblEtsySales", Quarter2)) = True Then
    MsgBox "No data for the second quarter."
    txtQ2.Value = ""
Else
    Q2 = DSum("[TotalReceived]", "tblEtsySales", Quarter2)
    txtQ2.Value = Q2
End If

2 个答案:

答案 0 :(得分:1)

您的表达式#01/04/2017#读作2017-01-04。

这样做:

Dim Q2 As Currency
Dim Quarter2 As String
Dim Date1 As Date
Dim Date2 As Date

Date1 = #04/01/2017# 
Date2 = #06/30/2017# ' You can type it differently, but VBA will correct it.

Quarter2 = "[Sale Date] BETWEEN #" & Format(Date1, "yyyy\/mm\/dd") & "# And #" & Format(Date2, "yyyy\/mm\/dd") & "#"

Q2 = DSum("[TotalReceived]", "tblEtsySales", Quarter2)

If Q2 = 0 Then
    MsgBox "No data for the second quarter."
    txtQ2.Value = Null
Else
    txtQ2.Value = Q2
End If

答案 1 :(得分:0)

您可能最好将数据参数传递给程序以使其更灵活,但如果您只需要将其用于永久性的......

Dim Q2 As Double
Dim Quarter2 As String

Quarter2 = "[Sale Date] BETWEEN #01/04/2017# And #30/06/2017#"
Q2=DSum("[TotalReceived]", "tblEtsySales", Quarter2)

if Q2=0 then
    MsgBox "No data for the second quarter."
    txtQ2.Value = ""
else
    txtQ2.Value=Q2
end if