Private Sub Form_Load()
Dim rsDLS As Recordset 'shows date mail was last sent
Dim rsEmail As DAO.Recordset 'the recordset with the email addresses to be used
Dim strEmail As String 'the email address
Dim strMessage As String 'message to be sent
Dim strSubject As String 'subject of message
Dim strJobNum As String 'the job posting number
Dim strJobTitle As String 'the title the job is listed under
Dim PD As Date 'the date the job was originally posted
Dim DLS As Date 'date emails were sent last
Set rsDLS = CurrentDb.OpenRecordset("tblDateLastSent") 'table where DLS is stored
rsDLS.MoveFirst 'makes sure correct record is selected
DLS = rsDLS.Fields("DateLastSent").Value 'sets value of DLS
If DLS = Date Then
Set rsDLS = Nothing
DoCmd.close 'if mail has already been sent, sub exits and form closes
Exit Sub
Else
Set rsEmail = CurrentDb.OpenRecordset("tbl27Days") 'opens table with email addresses
On Error Resume Next 'skips email addresses with errors
Do While Not rsEmail.EOF 'loops through until the end of the table
strEmail = rsEmail.Fields("E-mail").Value 'sets email address value
strJobNum = rsEmail.Fields("JobNumber").Value 'sets job number value
PD = rsEmail.Fields("PostDate").Value 'sets post date value
strJobTitle = rsEmail.Fields("JobTitle").Value 'sets job title value
strMessage = "Your job on the ISU job board, job #" & strJobNum & " entitled: " & strJobTitle & ", which was posted " & PD & ", will be removed on " & DateAdd("d", 30, PD) & " unless you contact us before that date. Thank you. -OSFA staff" 'message to be sent
strSubject = "Job Number " & strJobNum & " on the ISU job board" 'subject of message to be sent
DoCmd.SendObject , , , strEmail, , , strSubject, strMessage, False 'sends emails using above parameters
rsEmail.MoveNext 'goes to next record
Loop 'starts whole process over again
Set rsEmail = Nothing 'closes table
End If
DoCmd.close 'closes form when completed
End Sub