首页 > 解决方案 > 为了以不同的查询定义采用不同的参数的方式实施 MS Access Report,需要采取哪些步骤?

问题描述

在我的工作桌面上,我有

-Microsoft Office Professional Plus 2013 Access

我的任务是创建一个带有 Access DB 的 MS Access 应用程序。

我的应用程序中有一个 MS Access 报告

MS Access 报告的记录源与 MS Access 查询定义相关联

,此外,所述 MS Access 查询定义采用我命名为 idArg 的参数(类型为 Double )。

但是,在 MS Access 报告中,我还有一个 ListBox,它与另一个 MS Access 查询定义相关联,它采用不同的参数,我将其命名为 idArg2 (类型为 Double )。

上述 MS Access 报告最终将用于生成其自身的 pdf 版本。

我从表单的 VB 代码以编程方式生成 pdf:

DoCmd.SetParameter "idArg", CInt(Me.IdLabel.Caption)
DoCmd.SetParameter "idArg2", CInt(Me.IdV2.Caption)
DoCmd.OpenReport "OrgFinancialInstReport", acViewPreview
 DoCmd.OutputTo acOutputReport, "OrgFinancialInstReport", acFormatPDF, GetConstructedPdfFileName(CInt(Me.IdLabel.Caption), Me.InvestorName.Caption), True
  DoCmd.Close acReport, "OrgFinancialInstReport"

但是,上面显示的代码仅成功地将 CInt(Me.IdLabel.Caption) 正确应用于“idArg”,但未能将 CInt(Me.IdV2.Caption) 应用于“idArg2”

因此,当上面的代码运行时,我看到请求“idArg2”值的弹出框

为了实施所述 MS 访问报告,我必须采取哪些步骤才能正确地将 CInt(Me.IdV2.Caption) 分配给“idArg2”?

标签: sqlvbams-accessms-reports

解决方案


只需删除查询参数。只需将报告基于原始查询。

然后,过滤报告 - 传递一个“where”子句。

所以:

dim strWhere     as string
dim strReport    as string

strReport = "OrgFinancialInstReport"
strWhere = "Arg = " & CInt(me.IdLabel.Caption)
DoCmd.OpenReport strReport, acViewPreview,,strWhere

DoCmd.OutputTo acOutputReport, strReport, acFormatPDF, 
     GetConstructedPdfFileName(CInt(Me.IdLabel.Caption), Me.InvestorName.Caption), True
DoCmd.Close acReport, strReport

对于另一份报告,我们去:

dim strWhere     as string
dim strReport    as string

strReport = "OrgFinancialInstReport"
strWhere = "Arg2 = " & CInt(Me.IdV2.Caption)
DoCmd.OpenReport strReport, acViewPreview,,strWhere

DoCmd.OutputTo acOutputReport, strReport, acFormatPDF, 
     GetConstructedPdfFileName(CInt(Me.IdLabel.Caption), Me.InvestorName.Caption), True
DoCmd.Close acReport, strReport

您甚至可以将创建 PDF 的上述代码分解为一个单独的例程 - 从任何形式调用它 - 只需传递报告名称、where 子句,也许还有标题。

那么,当您不提前知道参数的数量时,或者它们会发生变化?只需使用 where 子句。因此,您的查询将没有参数。


推荐阅读