OK, can you try this
Code:
Private Sub Command11_Click()
'This will print the reports
Dim stEmailMessage As String
Dim stReport As String
Dim stCaption As String
Dim myPath As String
Dim strSQL As String
Dim stSubject As String
Dim rst As Recordset
Dim objRp As Report
'This sets the strings for each part of the email
stEmailMessage = "Please see the attached collateral call for today."
stReport = "Collateral Demand"
'This will change the caption of the report (which is what the filename is saved as" to something meaningful - in this case the numbers and date of thereport. If you have multiple people making these reports at once, and would have multiple ones with the same date/time stamp, you should modify this a little to prevent overwriting files.
myPath = "J:\Investments\Trade Operations\Derivative Transactions\Collateral\Access Database\"
strSQL = "SELECT [DECO Import File].Combo, [Counterparty Data].CPEmail, [Counterparty Data].PortfolioName, [Counterparty Data].BrokerName" & vbLf & _
"FROM [Counterparty Data] INNER JOIN [DECO Import File] ON [Counterparty Data].Combo = [DECO Import File].Combo" & vbLf & _
"WHERE ((([DECO Import File].[Collateral To Receive])>0));"
Set rst = CurrentDb.OpenRecordset(strSQL)
Set objRp = Reports(stReport)
While Not (rst.BOF And rst.EOF)
stSubject = "Collateral Demand - " & rst.Fields(2).Value & " - " & rst.Fields(3).Value & Format(Now(), " mm-dd-yyyy")
stCaption = stSubject
objRp.Caption = stCaption
RewriteQuerySQL "Report Query", rst.Fields(0).Value
DoCmd.OpenQuery "Report Query"
DoCmd.Close acQuery, "Report Query"
DoCmd.OpenReport "Collateral Demand", acViewNormal
DoCmd.SendObject acSendReport, stReport, acFormatPDF, rst.Fields(1).Value, "derivcollateral@rgare.com", , stSubject, stEmailMessage, True, ""
DoCmd.OutputTo acOutputReport, stReport, acFormatPDF, myPath & stCaption & ".pdf", False, , , acExportQualityPrint
rst.MoveNext
Wend
End Sub
Bookmarks