我有一些代码可以在访问中构建基本报告,但是,当我尝试使用变量 rpt 遍历所有报告时,它会跳过循环部分,因为没有为对象分配任何内容。有任何想法吗?我需要获取 rpt 才能找到标题为 qryDummy 的报告。提前致谢!:-)
Dim rptReport As Access.Report
Dim strCaption As String
Dim rpt As Report
CurrentDb.QueryDefs("qryDummy").SQL = strSQL
' Open dummy query to invoke NewObjectAutoReport command on it
' Put the report created to design view to make properties editable
With DoCmd
.OpenQuery "qryDummy", acViewNormal
.RunCommand acCmdNewObjectAutoReport
.Close acQuery, "qryDummy"
.RunCommand acCmdDesignView
End With
' Get reference to just created report
' !!!!!!!!!! This is the Section Giving me problems will !!!!!!!!!!!!!!
' !!!!!!!!!! not loop through all the reports. !!!!!!!!!!!!!!!!!!!!!!!!!
For Each rpt In Reports
If rpt.Caption = "qryDummy" Then Set rptReport = rpt
Next
With rptReport
' Create title control
With CreateReportControl(.Name, acLabel, _
acPageHeader, , ReportTitle, 0, 0)
.FontBold = True
.FontSize = 12
.SizeToFit
End With
' Create timestamp on footer
CreateReportControl .Name, acLabel, _
acPageFooter, , Now(), 0, 0
' Create page numbering on footer
With CreateReportControl(.Name, acTextBox, _
acPageFooter, , "='Page ' & [Page] & ' of ' & [Pages]", _
.Width - 1000, 0)
.SizeToFit
End With
' Detach the report from dummy query
.RecordSource = strSQL
' Set the report caption to autogenerated unique string
strCaption = GetUniqueReportName
If strCaption <> "" Then .Caption = strCaption
End With
DoCmd.RunCommand acCmdPrintPreview
Set rptReport = Nothing
编辑:
好的,所以我想我的问题将使用这段代码,因为当 VBA 运行时报告处于打开状态:
For Each rpt In Reports
If rpt.Caption = "qryDummy" Then Set rptReport = rpt
Next
我唯一的问题是它没有分配 rptReport = rpt 我得到错误:rpt = nothing,这导致 rpt.caption = "Object variable or with block variable not set"。所以就像没有看到公开报告一样?
仅供参考,解决了需要将 rpt.caption 更改为 rpt.Name 的问题 感谢您的帮助!