A
Arlan
I am looking for help, as I am ready to throw my laptop in the
trash...
The following code is used to open a query, send data about the user
to an excel template, save the template as a NEW file, then e-mail via
outlook. I am able to accomplish this by manually filtering the Query
used by my recordset, but I would truly like to be able to loop
through the records via code....
example...
filter for ID=1 (4 records)
Filter for ID=2 (4 records)
Whenever I try to apply the filter to the recordset...it bombs...any
assistance is appreciated. Thanks.
START CODE********
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
'From here I need to then re-query the recordset for the next ID#???
'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
trash...
The following code is used to open a query, send data about the user
to an excel template, save the template as a NEW file, then e-mail via
outlook. I am able to accomplish this by manually filtering the Query
used by my recordset, but I would truly like to be able to loop
through the records via code....
example...
filter for ID=1 (4 records)
Filter for ID=2 (4 records)
Whenever I try to apply the filter to the recordset...it bombs...any
assistance is appreciated. Thanks.
START CODE********
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
'From here I need to then re-query the recordset for the next ID#???
'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