按日期范围过滤SQL

时间:2017-05-18 08:15:20

标签: sql ms-access access-vba ms-access-2010

我需要在代码中添加一些SQL,但不完全确定在哪里添加它。

当前代码:

Me.List5.RowSource = "SELECT  TOP " & Nz(Me.Text9.Value, 0) & " RTI.CRN,
                              Sum(RTI.Credit) AS Credit,
                              Count(RTI.CRN) AS [Count]
                      FROM    RTI
                      GROUP BY  RTI.CRN
                      ORDER BY  Sum(RTI.Credit) DESC;"

我需要添加一个部件来搜索文本框中的日期。我想我需要以下代码:

AND [StatusDateTime] >= [Text1] AND <= [Text2]

我的日期代码是对的吗?我在哪里将它添加到原始SQL?

谢谢

2 个答案:

答案 0 :(得分:2)

该条件会过滤生成的集合,因此它位于WHERE子句中,其位置恰好位于FROM之后。

你可以这样写这个条件

[StatusDateTime] >= [Text1] AND [StatusDateTime] <= [Text2]

或此(更紧凑)方式

[StatusDateTime] between [Text1] and [Text2]

结果查询将是

Me.List5.RowSource = "SELECT TOP " & Nz(Me.Text9.Value, 0) & " RTI.CRN,
                             Sum(RTI.Credit) AS Credit,
                             Count(RTI.CRN) AS [Count]
                      FROM   RTI
                      WHERE  [StatusDateTime] between [Text1] and [Text2]
                      GROUP BY  RTI.CRN
                      ORDER BY Sum(RTI.Credit) DESC;"

答案 1 :(得分:0)

1)你应该在“FROM RTI”之后添加它

2)应该是

AND [StatusDateTime] >= [Text1] AND [StatusDateTime] <= [Text2]

或者

AND [StatusDateTime] BETWEEN [Text1] AND [Text2]