Hi Guys, I'm having some trouble piecing together some code. Below is my purpose:
We send monthly reports to each sales rep. These reports need to be in an excel spreadsheet, and the data specific to each sales rep. I want to be able to send this automatically out of access to each sales rep.
I'm running into two roadblocks with docmd.sendobject.
1. It doesn't allow for a way to filter the query before sending (to show only each sales rep)
2. It doesn't allow for an sql query.
the SQL query looks promising, but I can't seem to find a way to send it without saving it as a file first (can do if necessary, but It has no value)
Please see code below:
Thanks for any insight!
We send monthly reports to each sales rep. These reports need to be in an excel spreadsheet, and the data specific to each sales rep. I want to be able to send this automatically out of access to each sales rep.
I'm running into two roadblocks with docmd.sendobject.
1. It doesn't allow for a way to filter the query before sending (to show only each sales rep)
2. It doesn't allow for an sql query.
the SQL query looks promising, but I can't seem to find a way to send it without saving it as a file first (can do if necessary, but It has no value)
Please see code below:
Code:
Private Sub CmdSendExcelTable_Click()
On Error GoTo Error_Handler
Dim db As DAO.Database
Dim rs As DAO.Recordset 'to hold the Sales Rep ID's
Dim strSQL As String 'holds new query definition
Dim strContactID As String 'same as above
Dim strTo As String
Dim strCC As String
Dim strBody As String
Dim strSubject As String
Dim qdf As DAO.QueryDef
'set email string information
strCC = "EMAIL"
strBody = "Please Look into the attached Quotes. Clarify their status on the Excel document and send back. Thanks!"
strSubject = "Aging Quotes"
Set db = CurrentDb
Set rs = db.OpenRecordset("qrySalesReps")
'set query def
Set qdf = db.QueryDefs("qrySalesDataReport")
With rs
'Perform Function
Do Until .EOF
.MoveFirst
'set Value to use in Query
strContactID = .Fields("SalesRepID").Value
strSQL = qdf.SQL
Debug.Print strSQL
strSQL = Replace(strSQL, "ReplaceSalesRep", strContactID)
Debug.Print strSQL
'_____ORIGINAL THOUGHT PATH_____CHANGE A PARAMETER IN QUERY
'set parameter in query
'qdf.Parameters("SalesRepContactID").Value = strContactID
'set Send To
strTo = DLookup("Email", "tblSalesRepInfo", "SalesRepID = '" & strContactID & "'")
'send stuff
On Error Resume Next
DoCmd.SendObject acSendQuery, , acFormatXLS, strTo, strCC, , strSubject, strBody
.MoveNext
Loop
End With
Exit_Procedure:
Set rs = Nothing
Set db = Nothing
On Error Resume Next
Exit Sub
Error_Handler:
DisplayErr Err.Number, Err.Description, "frmRunReport", "GetSalesRepID()"
Resume Exit_Procedure
Resume
End Sub
Thanks for any insight!