我有一个表单,有2个文本框和1个按钮。文本框是Date From和Date To。单击该按钮时,会运行一些VBA代码循环查询结果(qryInvoicesBetweenDates),获取发票ID并生成发票的打印预览。问题是,我无法弄清楚如何在循环中传递报告当前的ID。我只需要给DoCmd.OpenReport和invoice_number变量。
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
非常感谢。
答案 0 :(得分:3)
您可以将where语句与OpenReport一起使用:
DoCmd.OpenReport stDocName, acPreview,,"ID=" & Rs!invoice_number
其中ID是报告中与Rs!invoice_number
对应的字段的名称。上面的示例是针对数字数据类型的,您需要引用文本数据类型。