1

我有一个查询,我想从内部为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某种原因没有设置查询参数一样。

4

1 回答 1

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#)
于 2013-10-29T21:10:23.657 回答