Quoting through a set of records to output a Ms-Access report to a pdf file

I am trying to get a pdf file using tenant_idwith docmd.outputTo. Unfortunately, this procedure creates the output pdf file, which is in one single tenant_id. If I delete the docmd.outputTolast parameter pathName & fileName, then it needs the file name through the dialog, and the output file will be filtered using tenant_id. Any help would be greatly appreciated.

Here's the invoice request: 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
+4
source share
2 answers

Since there are DoCmd.OutputTonot enough parameters for filtering, the best option is to create a report for the public function to get the current identifier.

eg.

' Function to both set and retrieve the current Tenant ID
Public Function CurTenantID(Optional SetTenantID As Long = 0) As Long

    Static TenantID As Long

    If SetTenantID > 0 Then
        TenantID = SetTenantID
    End If

    CurTenantID = TenantID

End Function

,

SELECT * FROM tblInvoice WHERE tenant_id = CurTenantID()

PDF SetTenantID:

Public Sub Output()

    Dim MyRs As DAO.Recordset
    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")

    With MyRs
        ' .MoveFirst -- unneeded after OpenRecordset()
        Do While Not .EOF
            fileName = "Invoice_" & todayDate & !tenant_id & ".pdf"
            Call CurTenantID(!tenant_id)
            DoCmd.OutputTo acOutputReport, "Invoice", acFormatPDF, pathName & fileName
            .MoveNext
        Loop
    End With

End Sub
+1

DoCmd.OpenReport , DoCmd.OutputTo :

ObjectName > > : , ObjectType .

With MyRs
    .MoveFirst
    Do While Not .EOF
        fileName = "Invoice_" & todayDate & !tenant_id & ".pdf"

        DoCmd.OpenReport "Invoice", acViewReport, , "[tenant_id] = " & !tenant_id, acHidden
        DoCmd.OutputTo acOutputReport, , acFormatPDF, pathName & fileName
        .MoveNext
    Loop
End With
+1

Source: https://habr.com/ru/post/1689304/


All Articles