MS Access:如何使生成个性化报告的宏起作用?

MS Access: How to Make a Macro Generating Individualized Reports Work?

我目前在 MS Access 2010 .mdb(Access 2002-'03 格式)中有一个宏。

宏打开报表并使用成对的 SetFilter 和 ExportWithFormatting。宏在第一个 2 或 3 后挂断。

我正在考虑放置延迟,但看来我必须使用 VBA。那是我唯一的选择吗?有什么建议或解决办法吗?

Function Copy_Of_Wellness()
On Error GoTo Copy_Of_Wellness_Err

    DoCmd.Hourglass True
    DoCmd.OpenReport "Wellness Plan Report", acViewReport, "", "", acIcon
    DoCmd.SetFilter "", "[Ind]=""Mary Jane""", ""
    DoCmd.OutputTo acOutputReport, "Wellness Plan Report", 
           "PDFFormat(*.pdf)", """M:\Mary.pdf""", True, "", , acExportQualityPrint
    DoCmd.SetFilter "", "[Ind]=""Howard Johnson""", ""
    DoCmd.OutputTo acOutputReport, "Wellness Plan Report", "PDFFormat(*.pdf)", """M:\Howard.pdf""", True, "", , acExportQualityPrint
    DoCmd.Hourglass False

Copy_Of_Wellness_Exit:
    Exit Function

Copy_Of_Wellness_Err:
    MsgBox Error$
    Resume Copy_Of_Wellness_Exit

End Function

尽管最初的错误表明了什么,但报告是有界的,并且一开始确实能很好地生成报告。

停点随意,可能运行时间错误?

脏修复:

  1. OpenReport()
  2. SetFilter()
  3. ExportWithFormatting()
  4. CloseWindow()

冲洗并根据需要重复。关闭并重新打开报告是我唯一可以解决的问题。 SelectObject() 并使用各种命令删除过滤器被证明是无效的。

根据文档考虑在 DoCmd.OpenReport and then passing an empty ObjectName argument in DoCmdOutputTo 中使用 WhereCondition 而不需要 DoCmd.SetFilter

ObjectName > Optional > Variant: ...If you want to output the active object, specify the object's type for the ObjectType argument and leave this argument blank...

VBA

调整了引号的使用,还删除了 acIcon,它可能会在启动外部任务栏操作时挂起处理

...
DoCmd.OpenReport "Wellness Plan Report", acViewReport, , "[Ind]='Mary Jane'"       
DoCmd.OutputTo acOutputReport, , acFormatPDF, "M:\Mary.pdf", True, , , acExportQualityPrint

DoCmd.OpenReport "Wellness Plan Report", acViewReport, , "[Ind]='Howard Johnson'"
DoCmd.OutputTo acOutputReport, , acFormatPDF, "M:\Howard.pdf", True, , , acExportQualityPrint
...

甚至考虑一个 DRY-er 方法的循环:

...
Dim var as Variant

For Each var in Array("Mary Jane", "Howard Johnson")
   DoCmd.OpenReport "Wellness Plan Report", acViewReport, , "[Ind]='" & var & "'",
   DoCmd.OutputTo acOutputReport, , acFormatPDF, "M:\'" & var & "'.pdf", True, , , acExportQualityPrint
Next var