Looping through a recordset to output Ms-Access re

2019-06-28 08:06发布

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

2条回答
趁早两清
2楼-- · 2019-06-28 08:28

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.

' 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

Your report uses this function in its record source, e.g.

SELECT * FROM tblInvoice WHERE tenant_id = CurTenantID()

And when generating the PDF reports in the loop, you use the SetTenantID parameter:

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
查看更多
可以哭但决不认输i
3楼-- · 2019-06-28 08:47

Simply use the DoCmd.OpenReport method to filter report and then leave the report name blank in DoCmd.OutputTo as per the docs:

ObjectName > Optional > Variant: If you want to output the active object, specify the object's type for the ObjectType argument and leave this argument blank.

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
查看更多
登录 后发表回答