Access 2007 VBA Do While Loop Repeats Output - TechRepublic
General discussion
June 6, 2011 at 04:38 PM
kb_whitmore

Access 2007 VBA Do While Loop Repeats Output

by kb_whitmore . Updated 15 years ago

I have a control table with various reports that I want to output to PDF. Using Access 2007 VBA, I wrote a Do While Loop to go through the local table (30 records) and print out each report. The problem is that is just prints out the same report 30 times. Can anyone tell me what is wrong with this code? I am very frustrated and would appreciate some advice.

Sub mod_TestPrint()
On Error GoTo mod_TestPrint_Err

Dim dbs As DAO.Database, rst As DAO.Recordset, StrRptName As String, strPath As String

Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(“TEmailControlPrc”, dbOpenTable, dbReadOnly)

StrRptName = rst!ReportName
strPath = rst!FilePath & Format(Date, “mmmm”) & rst!FileName

rst.MoveLast
rst.MoveFirst

Do While Not rst.EOF
DoCmd.OutputTo acOutputReport, StrRptName, “PDFFormat(*.pdf)”, strPath, False, “”, 0, acExportQualityPrint
rst.MoveNext
Loop

rst.Close

Set rst = Nothing
Set db = Nothing

MsgBox Prompt:=”Pricing Reports have been saved to pdf.”

mod_TestPrint_Exit:
Exit Sub

mod_TestPrint_Err:
MsgBox Error$
Resume mod_TestPrint_Exit
End Sub

Thanks KB

This discussion is locked

All Comments