簡體   English   中英

我可以輸入一個“?” 在傳遞查詢的日期范圍內,以便我可以在 Access 中的表單中輸入日期范圍?

[英]Can I enter a '?' in a date range in a pass-through query so I can enter a date range in a form in Access?

在訪問中有沒有辦法創建不保留硬編碼日期的傳遞查詢? 例如,我有一個鏈接到傳遞查詢的表單,我希望能夠通過表單輸入一個日期范圍並讓它更新傳遞查詢。 這是我當前的查詢,但我更喜歡它是一個帶有“?”的日期范圍。 或者允許提示輸入日期范圍或讓我通過表單輸入日期范圍的東西。 這能做到嗎? 還是我必須使用 VBA?

select distinct fill_sold_dt ,pf.str_nbr,wic_nbr, pkg_sz, pkg_qty, sum(fill_qty_dspn), sum((fill_qty_dspn)/(pkg_sz*pkg_qty) ) as Packs 
 from
prdedwvwh.prescription_fill_sold pf, prdedwvwh.location_store_address_cur lsa, prdedwvwh.drug_cur d
where pf.str_nbr=lsa.str_nbr and
d.drug_id=pf.drug_id
and fill_sold_dt(DATE) >= CURRENT_DATE - 7
 and wic_nbr in (683378,335776,418723)

group by 1,2,3,4,5

您必須使用 VBA。 如果您不知道如何在 VBA 中完成此操作,您可以查看此頁面上的示例 2: http : //support.microsoft.com/kb/131534

編輯- 添加完整示例

在表單上創建 2 個日期類型的文本框,並將它們命名為 DateStart 和 DateEnd

創建一個帶有單擊事件的按鈕,如下所示:

Private Sub Command0_Click()

    GetDataFromDate DateStart.value, DateENd.value

End Sub

並將此 Sub 添加到您的表單中:

Sub GetDataFromDate(dtStart As Date, dtEnd As Date)

 Dim MyDb As Database, MyQry As QueryDef, MyRS  As Recordset
 Set MyDb = CurrentDb()
 Set MyQry = MyDb.CreateQueryDef("")

 ' Type a connect string using the appropriate values for your
 ' server.
 MyQry.Connect = "ODBC;DSN=user1;UID=user1;PWD=user1;DATABASE=TEST"

 ' Set the SQL query with Date Range passed to the sub as parameters
 MyQry.SQL = " SELECT distinct fill_sold_dt ,pf.str_nbr,wic_nbr, pkg_sz, pkg_qty, sum(fill_qty_dspn) as som, sum((fill_qty_dspn)/(pkg_sz*pkg_qty) ) as Packs " & _
             " FROM prdedwvwh.prescription_fill_sold pf, prdedwvwh.location_store_address_cur lsa, prdedwvwh.drug_cur d " & _
             " WHERE pf.str_nbr=lsa.str_nbr and d.drug_id = pf.drug_id and wic_nbr in (683378,335776,418723)" & _
             "   AND fill_sold_dt BETWEEN #" & dtStart & "# AND #" & dtEnd & "# " & _
             " GROUP BY 1,2,3,4,5 "

 MyQry.ReturnsRecords = True
 Set MyRS = MyQry.OpenRecordset()

 MyRS.MoveFirst

 If Not MyRS.BOF Then

    While Not MyRS.EOF

        Debug.Print MyRS!fill_sold_dt
        Debug.Print MyRS!str_nbr
        Debug.Print MyRS!pkg_sz
        Debug.Print MyRS!som
        Debug.Print MyRS!Packs

        MyRS.MoveNext
    Wend


 End If

 MyQry.Close
 MyRS.Close
 MyDb.Close

End Sub

干得好

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM