循环遍历参数查询以创建PDF

时间:2019-04-23 21:49:39

标签: vba ms-access

我正在尝试遍历Access 2016中的参数查询。当前,参数是使用表单提供给查询的。那部分效果很好。我可以从PQData设置rs=CurrentDB.OpenRecordset("Select distinct [User_ID]”,dbOpenSnapshot,并且该报告将生成所有报告,无论我在参数中选择哪个团队。如果我使用Set rs=qdf.OpenRecordset(),则代码不会移至下一个UserID。我需要代码遍历参数查询,并且只为与团队参数匹配的UserID生成PDF。

Private Sub Command36_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset

Dim MyFileName As String
Dim mypath As String
Dim temp As String
Dim prm As DAO.Parameter
'Dim SSQL As String

mypath = "S:\Other\ClaimantSurveyDatabase\DB\Leave and Life Audits\Life  Audit Tool.accdb"

Set db = CurrentDb()


Set qdf = CurrentDb.QueryDefs("PQData Query")

qdf("Forms!frmCriteria!Month") = Forms!frmCriteria!Month
qdf("Forms!frmCriteria!EnterTeam") = Forms!frmCriteria!EnterTeam


Set rs = qdf.OpenRecordset()

'Set rs = CurrentDb.OpenRecordset("SELECT DISTINCT [USER_ID]  FROM [PQData]", dbOpenSnapshot)
 With rs
 .MoveFirst
  Do While Not .EOF
    temp = rs("USER_ID")
  MyFileName = rs("USER_ID") & ".PDF"


    DoCmd.OpenReport "LifeAuditSheets2019", acViewReport, , "[USER_ID]='" & temp & "'"
    DoCmd.OutputTo acOutputReport, "", acFormatPDF, mypath & MyFileName

    DoEvents

    rs.MoveNext
    Loop

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

     DoCmd.Close acReport, "LifeAuditSheets2019"


     End Sub

0 个答案:

没有答案