I am generating multiple pdfs from MS Access report, but files keep overwriting each other and the code seems to run forever. Please help. See Code below:
Code:
Option Compare Database
Private Sub Command0_Click()
Dim rs As DAO.Recordset
Dim sFolder As String
Dim sFile As String
On Error GoTo Error_Handler
sFolder = "D:\Journal\"
Set rs = CurrentDb.OpenRecordset("SELECT * FROM qryAllRecords", dbOpenSnapshot)
With rs
.MoveFirst
Do While Not .EOF
DoCmd.OpenReport "rptPreJournal", acViewPreview, , "[NRegistrationCentreid]=" & ![NRegistrationCentreid], acHidden
sFile = Nz(![District], "") & " - " & Nz(![NRegistrationCentreid], "") & ".pdf"
sFile = sFolder & sFile
DoCmd.OutputTo acOutputReport, "rptPreJournal", acFormatPDF, sFile
'If you wanted to create an e-mail and include an individual report, you would do so now
DoCmd.Close acReport, "rptPreJournal"
.MoveNext
Loop
End With
'Application.FollowHyperlink sFolder 'Optional / Open the folder housing the files
Error_Handler_Exit:
On Error Resume Next
If Not rs Is Nothing Then
rs.Close
Set rs = Nothing
End If
Exit Sub
Error_Handler:
If Err.Number <> 2501 Then 'Let's ignore user cancellation of this action!
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: cmd_GenPDFs_Click" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occured!"
End If
Resume Error_Handler_Exit
End Sub
Comment