保存具有不同名称的访问报告

时间:2019-05-15 20:46:10

标签: vba ms-access access-vba ms-access-reports

我正在尝试使用访问报告保存每个员工的对账单。但是,我在使Employee Name字段追加到MyFileName变量时遇到问题。

我可以将以下格式的个人员工报告导出为PDF:

Statement_[EENo].pdf

但是,我希望使用以下格式包括员工姓名:

Statement_[Employee Name]_[EENo].pdf
Private Sub Command2_Click()
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim MyFileName As String
    Dim mypath As String
    Dim temp As String

    mypath = "FILE LOCATION"

    Set db = CurrentDb()

    Set rs = db.OpenRecordset("SELECT distinct [EENo] & [Employee Name] FROM [Query]", dbOpenSnapshot)

    rs.MoveFirst
    Do While Not rs.EOF
        temp = rs("EENo" & "Employee Name")
        MyFileName = "Statement" & "_" & [Employee Name] & "_" & Format(rs("EENo"), "000000") & ".PDF"

        DoCmd.OpenQuery "1 - Query: Firm Admin_EE"
        DoCmd.Close acQuery, "1 - Query: Firm Admin_EE", acSaveYes
        DoCmd.OpenReport "REPORT", acViewReport, , "[EENo]=" & temp
        DoCmd.OutputTo acOutputReport, "", acFormatPDF, mypath & MyFileName
        DoCmd.Close acReport, "REPORT"
        DoEvents

        rs.MoveNext
    Loop

    rs.Close
    Set rs = Nothing
    Set db = Nothing
End Sub 

1 个答案:

答案 0 :(得分:0)

这应该有效:

temp = rs("EENo").Value & rs("Employee Name").Value
MyFileName = "Statement_" & rs("Employee Name").Value & "_" & _
              Format(rs("EENo").Value, "000000") & ".PDF"

编辑:我没有真正看过您的SQL,但是错过了您将查询中的两列结合在一起的情况。我不使用访问权限,但尝试:

Set rs = db.OpenRecordset("SELECT distinct [EENo] , [Employee Name] FROM [Query]", _
                           dbOpenSnapshot)