Hello everyone!
I’m new to the Access Forum and was hoping someone could help me.
Although I’ve been using Access for many, many years, I am a beginner with VBA. I found a web forum that referenced how to print multiple .pdf files from only one report. My report has 102 AFO_NAMEs, so I’m trying to avoid creating/printing 102 reports separately.
I was able to convert most of the code I found to my report and field names, and got my report to print into 102 different .pdf files. However, each report is blank with a #Type! error.
Just for some background, my report name is Rpts_by_AFO, and it is setup to Group by AFO_NAME. My actual query it pulls from is ALL-STATES.
Here is my code now…
______________________________
Function PrintReports()
Dim sql As String
Dim db As Database
Dim rs As Recordset
sql = " SELECT [AFO_NAME] FROM [ALL-STATES] GROUP BY [AFO_NAME]"
Set db = CurrentDb
Set rs = db.OpenRecordset(sql, dbOpenSnapshot)
Do Until rs.EOF
DoCmd.OpenReport "Rpts_by_AFO", acViewPreview, , sql = " & rs![AFO_NAME]"
DoCmd.OutputTo acOutputReport, "Rpts_by_AFO", "PDFFormat(*.pdf)", "C:\Desktop\Q1files\" & rs![AFO_NAME] & ".pdf", True
DoCmd.Close acReport, "Rpts_by_AFO"
DoEvents
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
Set db = Nothing
End Function
______________________________
Any help you can provide is much appreciated! Thank you!
I’m new to the Access Forum and was hoping someone could help me.
Although I’ve been using Access for many, many years, I am a beginner with VBA. I found a web forum that referenced how to print multiple .pdf files from only one report. My report has 102 AFO_NAMEs, so I’m trying to avoid creating/printing 102 reports separately.
I was able to convert most of the code I found to my report and field names, and got my report to print into 102 different .pdf files. However, each report is blank with a #Type! error.
Just for some background, my report name is Rpts_by_AFO, and it is setup to Group by AFO_NAME. My actual query it pulls from is ALL-STATES.
Here is my code now…
______________________________
Function PrintReports()
Dim sql As String
Dim db As Database
Dim rs As Recordset
sql = " SELECT [AFO_NAME] FROM [ALL-STATES] GROUP BY [AFO_NAME]"
Set db = CurrentDb
Set rs = db.OpenRecordset(sql, dbOpenSnapshot)
Do Until rs.EOF
DoCmd.OpenReport "Rpts_by_AFO", acViewPreview, , sql = " & rs![AFO_NAME]"
DoCmd.OutputTo acOutputReport, "Rpts_by_AFO", "PDFFormat(*.pdf)", "C:\Desktop\Q1files\" & rs![AFO_NAME] & ".pdf", True
DoCmd.Close acReport, "Rpts_by_AFO"
DoEvents
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
Set db = Nothing
End Function
______________________________
Any help you can provide is much appreciated! Thank you!