Hey all, I have this piece of code that I put together that loops through a recordset of emails and changes the reports querydefs WHERE clause on each loop for the particular user.
It works and sends out the correct report with the correct info...I'm just looking to do 2 things:
1. I'm not the best VBA writer, so what can I do to cut down on some code or improve it.
2. Keep Outlook from quacking about the "sending on your behalf...allow or deny" message. (I looked into the CDO but am not sure how to get it to send the attachments as smoothly as how I'm doing it now.)
Keep in mind I'm using Access 2007.
Basically I'm calling a source sql string and trimming the ; off the end of it. I'm then looping through adding a where clause and then sending it off using the SendObject command. Before the loop starts over, I'm resetting my sql to the source again without any where clauses on it.
Thoughts?
It works and sends out the correct report with the correct info...I'm just looking to do 2 things:
1. I'm not the best VBA writer, so what can I do to cut down on some code or improve it.
2. Keep Outlook from quacking about the "sending on your behalf...allow or deny" message. (I looked into the CDO but am not sure how to get it to send the attachments as smoothly as how I'm doing it now.)
Keep in mind I'm using Access 2007.
Code:
Private Sub cmdEmail_Click()
Dim db As DAO.Database
Dim qd As DAO.QueryDef
Dim rptqd As DAO.QueryDef
Dim strSQL As String
Dim rst As Recordset
Set db = CurrentDb
Set qd = db.QueryDefs("qryRptItems")
Set rptqd = db.QueryDefs("qryReportSource")
strSQL = qd.SQL
Set rst = db.OpenRecordset("qryEmailList")
Do Until rst.EOF = True
strSQL = Left(strSQL, Len(strSQL) - 3) & " WHERE UserName = '" & rst!UserName & "'"
rptqd.SQL = strSQL
DoCmd.SendObject acSendReport, "rptItems", acFormatPDF, rst("UserEmail"), , , "Auto Email", "Test", False
strSQL = qd.SQL
rst.MoveNext
Loop
rst.Close
End Sub
Basically I'm calling a source sql string and trimming the ; off the end of it. I'm then looping through adding a where clause and then sending it off using the SendObject command. Before the loop starts over, I'm resetting my sql to the source again without any where clauses on it.
Thoughts?