NZ date query doesn't work

noahjohn 41 Reputation points
2021-04-19T03:28:23.97+00:00

I have a code that generates and save a report as a pdf. I need to name the pdf as [lastname], [firstname] - [payroll_date] . My code works if it's only last name and first name. If I added the payroll_date , it will not generate and save the pdf.

Heres my code:

Set rs = CurrentDb.OpenRecordset("SELECT [ID],[last_name],[first_name],[payroll_date] FROM [Payslip_upload]", dbOpenSnapshot)
With rs
    If .RecordCount <> 0 Then    'Make sure we have record to generate PDF with
        'Open the Report
        DoCmd.OpenReport sReportName, acViewPreview, , , acHidden
        'Define a report object so we can manipulate it below
        Set rpt = Reports(sReportName).Report
        .MoveFirst
        Do While Not .EOF
            'Build the PDF filename we are going to use to save the PDF with

            sFile = Nz(![last_name], "") & ", " & Nz(![first_name], "") & Nz(![payroll_date], "") & ".pdf"
            sFile = sFolder & sFile
            'filter the report to the specific record or criteria we want
            rpt.Filter = "[ID]=" & ![ID]
            rpt.FilterOn = True
            DoEvents 'This is critical!!!!
            'Print it out as a PDF
            DoCmd.OutputTo acOutputReport, sReportName, acFormatPDF, sFile, , , , acExportQualityPrint
Developer technologies | Visual Basic for Applications
0 comments No comments
{count} votes

Answer accepted by question author
  1. Viorel 125.6K Reputation points
    2021-04-19T04:40:17.983+00:00

    Probably payroll_date contains some characters, like ‘/’, which are not allowed. Try replacing the characters, for example:

    . . .
    Dim d as String
    d = Replace(Replace(Replace(Nz(![payroll_date], ""), "/", "-"), ".", "-"), ":", "-")
    sFile = Nz(![last_name], "") & ", " & Nz(![first_name], "") & d & ".pdf"
    . . .
    

    If this does not help, then show some sample values of payroll_date.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.