2013-10-29 115 views
1

我有一個查詢我想從內設置查詢參數爲DLookupDlookup與日期標準查詢參數?

查詢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

如果'checkDate_Count_ForDangerousTickets'是參數查詢,除非你適應查詢從表單,表格,或自定義VBA函數獲取它的參數值,你不能用它從'DLookup'。 – HansUp

+0

@HansUp我也一直在琢磨着,我也得出了同樣的結論。我開始認爲可能需要使用'QueryDef'對象的VBA函數。 –

回答

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#)