Dlookup与日期条件查询参数?

时间:2013-10-29 19:46:39

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

我有一个查询,我希望在DLookup内设置查询参数:

查询 checkDate_Count_ForDangerousTickets:

SELECT count(*) AS numRelatedTickets
FROM DangrousTicketList
WHERE DateValue(backUpDate)>=DateValue(StartDate) And DateValue(backUpDate)<=DateValue(EndDate)
GROUP BY backUpDate;

我从dlookup设置查询参数backUpDate

调用查询
DLookUp("[numRelatedTickets]", "[checkDate_Count_ForDangerousTickets]", _ 
"[backUpDate]=" & Format("01-27-2013", "\#mm\/dd\/yyyy\#"))

我收到错误:

Run-time Error '2471'
The expression you entered as a query parameter produced this error: 
'backUpDate'

好像查询参数backUpDate由于某种原因没有设置。

1 个答案:

答案 0 :(得分:3)

基于HansUp的评论和我自己的修补,我相信你将不得不使用一点VBA函数来运行你的参数查询:

Public Function LookupDangerousTicketCount(backUpDate As Date) As Variant
    Dim qdf As DAO.QueryDef, rst As DAO.Recordset
    Set qdf = CurrentDb.QueryDefs("checkDate_Count_ForDangerousTickets")
    qdf!backUpDate = backUpDate
    Set rst = qdf.OpenRecordset(dbOpenSnapshot)
    If rst.BOF And rst.EOF Then
        LookupDangerousTicketCount = Null
    Else
        LookupDangerousTicketCount = rst!numRelatedTickets
    End If
    rst.Close
    Set rst = Nothing
    Set qdf = Nothing
End Function

然后您可以使用

代替DLookup()来电
LookupDangerousTicketCount(#2013-01-27#)