将参数从DoCmd.OpenReport传递到Access 2010报表 [英] Pass parameters to Access 2010 report from DoCmd.OpenReport

查看:663
本文介绍了将参数从DoCmd.OpenReport传递到Access 2010报表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,其中有2个文本框和1个按钮.文本框是日期自"和日期至".单击该按钮后,将运行一些VBA代码,该代码循环遍历查询结果(qryInvoicesBetweenDates),获取发票ID并生成发票的打印预览.问题是,我无法解决如何在循环中将当前ID传递给报告.我只需要给DoCmd.OpenReport他invoice_number变量即可.

I have a form, which has 2 text boxes and 1 button. The text boxes are Date From and Date To. When the button is clicked, some VBA code runs which loops through query results (qryInvoicesBetweenDates), gets the Invoice ID and generates a print preview of the invoice. The problem is, I cant work out how to pass the report the current ID within the loop. I simply need to give the DoCmd.OpenReport he invoice_number variable.

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

非常感谢.

推荐答案

您可以将where语句与

You can use a where statement with OpenReport:

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

其中ID是报告中与Rs!invoice_number相对应的字段的名称.上面的示例是针对数字数据类型的,对于文本数据类型,您需要用引号引起来.

Where ID is the name of the field in the report that corresponds to Rs!invoice_number. The above example is for a numeric data type, you would need quotes for a text data type.

这篇关于将参数从DoCmd.OpenReport传递到Access 2010报表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆