I'm trying to get pdf file by tenant_id
using docmd.outputTo
. Unfortunately this routine produce output pdf file that all are in a same single tenant_id
. If I remove docmd.outputTo
last parameter pathName & fileName
then it's require file name through dialogue and output file nicely filtered by tenant_id
. Any help would be appreciated.
Here's the query of invoice: SELECT * FROM tblInvoice WHERE tenant_id = CurTenantID()
Public Sub Output()
Dim MyRs As DAO.Recordset
Dim rpt As Report
Dim fileName As String, pathName As String, todayDate As String
pathName = "C:\Users\abzalali\Dropbox\tenant_db\Invoice\"
todayDate = Format(Date, "MMDDYYYY")
Set MyRs = CurrentDb.OpenRecordset("SELECT tenant_id, name, company, email FROM qryEmailClientList")
DoCmd.OpenReport "Invoice", acPreview, , , acHidden
Set rpt = Reports("Invoice")
With MyRs
.MoveFirst
Do While Not .EOF
fileName = "Invoice_" & todayDate & !tenant_id & ".pdf"
rpt.Filter = "[tenant_id] = " & !tenant_id
rpt.FilterOn = True
DoCmd.OutputTo acOutputReport, "Invoice", acFormatPDF, pathName & fileName
.MoveNext
Loop
End With
End Sub
Since
DoCmd.OutputTo
lacks parameters for filtering, the best option is to base the report on a public function to get the current ID.E.g.
Your report uses this function in its record source, e.g.
And when generating the PDF reports in the loop, you use the
SetTenantID
parameter:Simply use the DoCmd.OpenReport method to filter report and then leave the report name blank in DoCmd.OutputTo as per the docs: