Thanks for the input, but I will be honest..you lost me...The
following is my code so far..I have been able to manually filter the
base query (qryIncentiveReport) and send this data to an excel
template then save and e-mail, but, what I need to do is via code,
filter for each user, then send that data to the excel template, save
and e-mail, then go onto the next user and filter, send data then save
and e-mail...etc.... Any pointers would be appreciated...Thanks
Function MoveData()
Dim objXL As Object
Dim objBook As Object
Dim objSheet As Object
Dim rsData As Recordset
Dim rsFilter As Recordset
Dim i As Integer
Dim i2 As Integer
Dim straddress As String
Dim appOutlook As New Outlook.Application
Dim objItem As Outlook.MailItem
Dim strExcelPath As String
Dim stDocName As String
stDocName = "qryIncentiveReport1"
'This represents the PK filed to filter for
i2 = 1
Set rsData = CurrentDb.OpenRecordset(stDocName, dbOpenSnapshot)
straddress = rsData!userid
'This is where I am lost....
rsData.Filter = "[ID]=" & i2
Set rsFilter = rsData.OpenRecordset
'I now need to Filter this record set, as I need to
'send each users worksheet to them seperately
'Each users worksheet will have between 3 and 6 records per
'spreadsheet.
If rsFilter.EOF = False Then
Set objXL = CreateObject("Excel.Application")
objXL.Visible = True
Set objBook = objXL.Workbooks.Open("C:\XLS\IncReport.xls")
Set objSheet = objBook.worksheets("template")
objSheet.Activate
i = 9
With rsFilter
.MoveFirst
Do Until .EOF
objSheet.Range("H3") = !userid
objSheet.Range("B5") = !desc
objSheet.Range("B6") = !PeriodYr
objSheet.Range("B7") = !flight
objSheet.Range("A" & i) = !week
objSheet.Range("B" & i) = !Sales
objSheet.Range("C" & i) = !Lines
objSheet.Range("D" & i) = !stops
objSheet.Range("E" & i) = !linesperstop
objSheet.Range("F" & i) = !minsales
objSheet.Range("G" & i) = !targsales
objSheet.Range("H" & i) = !outsales
objSheet.Range("I" & i) = !minlineinc
objSheet.Range("J" & i) = !targlineinc
objSheet.Range("K" & i) = !outlineinc
i = i + 1
.MoveNext
Loop
End With
'ReName the worksheet
objXL.Sheets("template").Name = "Sales Incentive Info"
'Save the workbook with the user ID PD and YR
ActiveWorkbook.SaveAs Filename:="C:\XLS\" & Range("H3") & "" &
Range("B6") & ".xls"
'Set the Path to the newly created workbook for e-mail
strExcelPath = "C:\XLS\" & Range("H3") & "" & Range("B6") & ".xls"
'Create New Mail Message
Set objItem = appOutlook.CreateItem(olMailItem)
With objItem
.To = straddress
.Subject = "Sales Incentive Criteria"
.Attachments.Add strExcelPath
.Display
'.Send
End With
Else
MsgBox "There was a problem"
End If
rsData.Close
Set rsData = Nothing
Set objSheet = Nothing
objBook.Close
Set objBook = Nothing
objXL.Quit
Set objXL = Nothing
End Function
"solex" <
[email protected]> wrote in message
Arlan,
Not sure why you need to "loop through a query and filter out records based
on another query" when you can simply perform this operation in one query
based on both of your base queries. My suggestion is that you create a
variant array based on the single query use the GetRow method of DAO or ADO
and transpose the array then use simple automation to set the range value of
the excel sheet equal to the variant array. Your data will be in Excel in
short order then you can use more automation to execute the "Send To Mail
Recipient As Attachment" option in the Excel Object Model.
The "Programming Excel" topic in the Excel on-line help has more
information.
Dan