DateSerial函数在SQL查询

时间:2015-10-19 20:23:48

标签: access-vba

此功能正确返回日期30 Sep 15:

Public Function ToDate() As Date

Dim MyYr As Integer

Select Case DatePart("m", Date)
        Case 1
               MyYr = DatePart("yyyy", Date) - 1
        Case Else
               MyYr = DatePart("yyyy", Date)
End Select

ToDate = DateSerial(MyYr, cmbMth, 1) - 1

End Function

但是如果我试着把它放在这里它就行不通了:

Me.RecordSource = "SELECT tblDisclosure.DBSInvoice, " & _
  "Sum(tblDisclosure.DBSFee) AS SumOfDBSFee, " & _
  "Sum(tblDisclosure.MyFee) AS SumOfMyFee, " & _
  "Count(tblDisclosure.ID) AS CountOfID, tblDisclosure.DBSInvoice " & _
  "From tblDisclosure " & _
  "GROUP BY tblDisclosure.DBSInvoice, tblDisclosure.DBSInvoice " & _
  "HAVING (((tblDisclosure.DBSInvoice) Is Not Null) AND " & _
      "((tblDisclosure.DBSInvoice) Between #10/31/2014# And ToDate)) "

我收到消息输入参数值 - ToDate

如果我将ToDate替换为DateSerial(2015,9,30)

,它确实有用

2 个答案:

答案 0 :(得分:2)

或者用DateSerial替换今天:

tblDisclosure.DBSInvoice Between #10/31/2014# And DateSerial(Year(Date()), Month(Date()),0)

答案 1 :(得分:1)

试试这个:

"HAVING (tblDisclosure.DBSInvoice Is Not Null) AND
(tblDisclosure.DBSInvoice Between #10/31/2014# And ToDate()) "

作为函数调用的一部分,差异是双括号:ToDate()