neilsolaris
Member
- Local time
- Today, 20:07
- Joined
- Apr 19, 2020
- Messages
- 114
I know I'm marked this thread as solved, but I've got a couple of small questions to ask. I've made a slight adjustment to the code (see below) in order to save only the last record as a PDF file.
So my first question is, is the way I've gone about it the most efficient? Do I really need a loop to achieve this?
Secondly, I'd like the user (me!) to input an invoice number (or to type letter L to indicate last record) and then it saves that particular record to PDF. What's the easiest way to go about saving a particular record based on it's invoice number?
Many thanks for your help.
So my first question is, is the way I've gone about it the most efficient? Do I really need a loop to achieve this?
Secondly, I'd like the user (me!) to input an invoice number (or to type letter L to indicate last record) and then it saves that particular record to PDF. What's the easiest way to go about saving a particular record based on it's invoice number?
Many thanks for your help.
Code:
Private Sub Command0_Click()
Dim rs As DAO.Recordset
Dim sFolder As String
Dim sFile As String
On Error GoTo Error_Handler
sFolder = "D:\Documents\Orchestra\Invoices\Invoice files\"
Set rs = CurrentDb.OpenRecordset("SELECT Invoice_Number FROM Q_Invoices", dbOpenSnapshot)
With rs
.MoveLast
Do While Not .EOF
DoCmd.OpenReport "R_Invoices_PDF", acViewPreview, , "[Invoice_Number]=" & ![Invoice_Number], acHidden
sFile = Nz(![Invoice_Number], "") & ".pdf"
sFile = sFolder & sFile
DoCmd.OutputTo acOutputReport, "R_Invoices_PDF", acFormatPDF, sFile
'If you wanted to create an e-mail and include an individual report, you would do so now
DoCmd.Close acReport, "R_Invoices_PDF"
.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
Last edited: