2013-02-18 82 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报告中的字段的名称。上面的例子是针对数字数据类型的,您需要引用文本数据类型。

相关问题