访问报告,每组打印pdf

时间:2015-03-02 19:03:56

标签: loops access-vba pdf-generation ms-access-2007

我仍然是VBA的新手。我一直绞尽脑汁,看着每个试图找到如何做到这一点的论坛。这是我到目前为止最接近的。 我有一个名为Streets的表,报告基于该表,显示每条街道的房屋数量。每条记录都位于名为StreetPrintOuts的报告的单个页面上。我将街道分成不同的区域,这些区域(MapNum)都编号。我希望每个pdf显示其所在地区的每条街道的所有街道记录。到目前为止,我有这个:

Public Sub PDFGroup_Click()
Const Folder = "C:\Users\Records\"
Const Domain = "Streets"
'Domain can be table name, query name, or sql statement that provides the values to loop
Const LoopedField = "MapNum"
Const ReportName = "StreetPrintOuts"

Dim rs As DAO.Recordset
Dim LoopedFieldValue As Long
Dim FileName As String
Dim FullPath As String
Dim strWhere As String
Set rs = CurrentDb.OpenRecordset(Domain)

Do While Not rs.EOF
LoopedFieldValue = rs.Fields(LoopedField)
FileName = LoopedFieldValue & ".pdf"
'The field may be a text field. It then must be surrounded in singlequotes. If so uncomment below
'LoopedFieldValue = "'" & LoopedFieldValue & "'"
FullPath = Folder & FileName
strWhere = LoopedField & " = " & LoopedFieldValue
Debug.Print FullPath
Debug.Print strWhere
DoCmd.OpenReport ReportName, acViewPreview, , strWhere
DoCmd.OutputTo acOutputReport, ReportName, acFormatPDF, FullPath
DoCmd.Close acReport, ReportName
rs.MoveNext
Loop

End Sub

每次运行时,都会在

之后停止
DoCmd.OpenReport ReportName, acViewPreview, , strWhere

错误代码运行时2501错误:输出操作已取消

我做错了什么?

1 个答案:

答案 0 :(得分:0)

此代码确实有效。我留在另一个变量中,使其在报告的其他地方中断。显然工作到深夜,错过了一些东西。