2013-02-18 78 views
0

我有一個表單,其中有2個文本框和1個按鈕。文本框是日期從和日期到。單擊按鈕時,會運行一些VBA代碼,這些代碼循環查詢結果(qryInvoicesBetweenDates),獲取發票ID並生成發票的打印預覽。問題是,我無法解決如何在循環中傳遞當前ID的報告。我只需要爲他的invoice_number變量提供DoCmd.OpenReport。將參數傳遞給DoCmd.OpenReport的Access 2010報告

VBA代碼:

Dim qdf As QueryDef 
Set qdf = CurrentDb.QueryDefs("qryInvoicesBetweenDates") 
Dim rs As DAO.Recordset 

qdf.Parameters(0) = tbFrom.Value 
qdf.Parameters(1) = tbTo.Value 

Set rs = qdf.OpenRecordset() 
Do Until rs.EOF = True 
     ' Set the invoice number to the current row 
     'invoice_number = rs.N 
    invoice_number = rs.Fields(0).Value 


    ' Preview the invoice 
    Dim stDocName As String 
    stDocName = "InvoiceForWork" 
    DoCmd.OpenReport stDocName, acPreview 
Loop 

非常感謝。

回答

3

您可以使用where語句OpenReport

DoCmd.OpenReport stDocName, acPreview,,"ID=" & Rs!invoice_number 

其中ID是對應於Rs!invoice_number報告中的字段的名稱。上面的例子是針對數字數據類型的,您需要引用文本數據類型。

相關問題