MS Access VBA正在创建多个报告电子邮件参数未存储引用

问题描述 投票:0回答:1

我正在研究一个报表,以将结束日期在表单上两个未绑定文本字段之间的员工拉出。该报告分组在主管上。我的目标是将每个报告分组保存为唯一的PDF,PDF名称为[Supervisor]-John Smith.PDF,Jill Smith.pdf等。此报告附加到电子邮件中,该电子邮件将打开并等待调整后再发送。

当前,除了我要解决的几个问题之外,这是可行的:

  1. 在每次打开报告之前,我都会看到一个参数弹出窗口,要求StartDate和EndDate(我的参数设置为表单文本字段)。我必须在每个报告之前为每个参数手动输入该日期,而不是从表单中填充。

  2. 我为每个主管打开了多封电子邮件。有些报告为空。似乎我为每个报告的每个记录获取一个,并为每个报告弹出参数。

  3. 按下按钮后,我将日期输入到参数弹出窗口中。我的报告将打开,保存,关闭,并正确生成电子邮件。不必自动移动到下一个报告/电子邮件,我必须手动将日期再次输入到参数弹出窗口中。

我的表单具有两个字段Text0和Text2,以及一个按钮,该按钮将使用VBA根据以下查询打印报告。

查询SQL:

SELECT DISTINCT [Active and Expired Badges].Supervisor_Clean, [Active and Expired Badges].FIRSTNAME, [Active and Expired Badges].LASTNAME, [Active and Expired Badges].[Premera ID], [Active and Expired Badges].Company, [Active and Expired Badges].Title, [Active and Expired Badges].[End Date], [FIRSTNAME] & " " & [LASTNAME] AS Name
FROM [Active and Expired Badges]
WHERE ((([Active and Expired Badges].Title) Like "*" & "outsource" & "*" Or ([Active and Expired Badges].Title) Like "*" & "Contingent" & "*") AND (([Active and Expired Badges].[End Date]) Between [StartDate:] And [StopDate:]));

按钮VBA:

Private Sub Command5_Click()

'split report into PDFs named after supervisor and open a separate email with each report attached

    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim MyFileName As String
    Dim mypath As String
    Dim temp As String
    Dim qry As QueryDef
    Dim StartDate As DAO.Parameter
    Dim StopDate As DAO.Parameter

    Set db = CurrentDb()
    Set qry = db.QueryDefs("30-Day query")
    mypath = "C:\Users\cw52450\Desktop\Test Exports\"
    qry.Parameters("StartDate").Value = [Forms]![EndDate]![Text0]
    qry.Parameters("StopDate").Value = [Forms]![EndDate]![Text2]

    Set rs = qry.OpenRecordset(dbOpenSnapshot)  
    If Not (rs.EOF And rs.BOF) Then

'populate rs

    rs.MoveLast
    rs.MoveFirst

'start report generation loop

    Do While Not rs.EOF       

        temp = rs("Supervisor_Clean")
        MyFileName = rs("Supervisor_Clean") & Format(Date, ", mmm yyyy") & ".PDF"
        DoCmd.OpenReport "End Date Report", acViewReport, , "[Supervisor_Clean]='" & temp & "'"
        DoCmd.OutputTo acOutputReport, "", acFormatPDF, mypath & MyFileName
        DoCmd.Close acReport, "End Date Report"
        DoEvents

'start mail section

    open Outlook, attach zip folder or file, send e-mail
    Dim appOutLook As Outlook.Application
    Dim MailOutLook As Outlook.MailItem
    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(olMailItem)

    With MailOutLook
        .SendUsingAccount = appOutLook.Session.Accounts.Item(2)
        .BodyFormat = olFormatRichText
        '.To = ""
        ''.cc = ""
        ''.bcc = ""
        .Subject = "Non-Employees with Expiring Building Access"
       .HTMLBody = "<html><body><p>Hello,</p><p>The attached report... </p></body></html>"
       .Attachments.Add (mypath & MyFileName)
        '.DeleteAfterSubmit = True 'This would let Outlook send the note without storing it in your sent bin
        .Display
    End With
'end mail section

        rs.MoveNext
    Loop
Else
    MsgBox "There are no records in the recordset."
End If
    MsgBox "Report generation complete."
    Set rs = Nothing
    Set db = Nothing
    Set qry = Nothing
End Sub
excel vba ms-access access-vba
1个回答
0
投票

弹出参数可能是由于此处语法不正确:

qry.Parameters("StartDate").Value = [Forms]![EndDate]![Text0]
qry.Parameters("StopDate").Value = [Forms]![EndDate]![Text2]

尝试将其更改为:

qry.Parameters("StartDate").Value = Me.Text0
qry.Parameters("StopDate").Value = Me.Text2
© www.soinside.com 2019 - 2024. All rights reserved.