Thanks for the reply! I am doing all of what you said here except instead of grouping I'm using a SELECT DISTINCT and writing all the unique values to an array and using each array value in a new recordset query.
I am having one issue though - I am able to find all the unique values and run the second query. Once I run it, it finds all the records with that criteria and writes them to an excel file. It then creates an email and attaches the file. However, on the next iteration of the loop I get a runtime error 424 "Object required" on this line: "r.copyfromrecordset rsXL2"
Any help? I suspect there is an issue of opening a new workbook on the second iteration, but I could be wrong.
Public Sub ExpirationSub()
DoCmd.SetWarnings (WarningsOff)
DoCmd.OpenQuery "qryExpirations"
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim rstXL As DAO.Recordset
Dim rs As DAO.Recordset
Dim rsXL As DAO.Recordset
Dim rsXL2 As DAO.Recordset
Dim uRS As DAO.Recordset
Dim objMessage
Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT * FROM tblEmailOut WHERE Says = 1")
Set rstXL = db.OpenRecordset("SELECT [Full Name],SOEID,[WO End Date],GOC,[Business Contact SOEID] FROM Actions WHERE [WO End Date] Between Date() and Date()+1")
Dim dPath As String
dPath = "I:\My Documents\Projects\R2\Task 4 - GSDS Contractor DB\"
Dim x As Object
Dim w As Excel.Workbook
Dim s As Excel.Worksheet
Dim r As Excel.Range
Set x = CreateObject("Excel.Application")
Set w = x.Workbooks.Open(dPath & "Expiring Resources Template.xls")
Set s = w.Sheets(1)
Set r = s.Range("A2")
Dim uGOC() As String
Dim uFileNm, uFileDt, fnGOC As String
Dim c, i, j As Integer
j = 1
c = 1
i = 1
'On Error Resume Next
'Get Outlook if it's running
Set oApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
'Outlook wasn't running, start it from code
Set oApp = CreateObject("Outlook.Application")
Started = True
End If
Do While Not rst.EOF
'--------------Create Attachment--------------
If i = 1 Then
Set uRS = db.OpenRecordset("SELECT DISTINCT [GOC] FROM Actions WHERE [WO End Date] Between Date() and Date()+1")
Do While c <= uRS.RecordCount
If uRS!GOC <> "" Then
ReDim Preserve uGOC(c)
uGOC(c) = uRS!GOC
End If
uRS.MoveNext
c = c + 1
Loop
End If
fnGOC = uGOC(j)
Set rsXL2 = db.OpenRecordset("SELECT * FROM Actions WHERE [WO End Date] Between Date() and Date()+1 AND GOC=" & Chr(34) & fnGOC & Chr(34) & "")
r.CopyFromRecordset rsXL2
s.Columns("A:I").EntireColumn.AutoFit
s.Columns("A:I").Font.Size = 10
uFileNm = dPath & "Expirations-" & fnGOC & "-" & (Format(Date, "mmddyyyy")) & uFileDt
w.SaveAs FileName:=uFileNm, FileFormat:=56
strPath = uFileNm & ".xls"
w.Close False
x.Quit
'---------------------------------------------
Set oItem = oApp.CreateItem(olMailItem)
With oItem
.To = rst!EmailAddress
.Subject = "PLEASE READ: Impending Resource Expiration Notification"
.Body = rst!EmailBody
.Importance = olImportanceHigh
.Attachments.Add (strPath)
'Send the email
'.Send
.Save
.Close olPromptForSave
End With
rst.MoveNext
i = i + 1
j = j + 1
Loop
'db.Execute "UPDATE Actions SET [ApprovalSent] = Date() WHERE DateDiff(""d"", Date(), [WO End Date]) = 1"
rst.Close
Set rst = Nothing
Set db = Nothing
Set oItem = Nothing
Set r = Nothing
Set s = Nothing
Set w = Nothing
Set x = Nothing
If Started Then
oApp.Quit
End If
End Sub
Bookmarks