'Send Email For Email Based on Due Date Microsoft Access

anyone could help me to create a button that can send emails to emails that are stored in the record set? I have created a Query that shows only records in which the due date is coming. Then, I created a form, to show the query results. I also have a button to send emails to all the record's email addresses.

Private Sub SendEmailBtn_Click()

Dim oOutlook As Outlook.Application
Dim oEmailItem As MailItem

Dim rs As Recordset
Set rs = CurrenDb.OpenRecordset("SELECT * FROM QryOverdue")

If rs.RecordCount > 0 Then
    rs.MoveFirst
    Do Until rs.EOF
    If IsNull(rs!EMAIL) Then
        rs.MoveNext
    Else
        If oOutlook Is Nothing Then
        Set oOutlook = New Outlook.Application
        End If
        Set oEmailItem = oOutlook.CreateItem(olMailItem)
        With oEmailItem
            .To = rs!EMAIL
            .Subject = "Reminder to Return your Book"
            .Body = "Dear " & rs!NAME & vbCr & _
                    "Name: " & rs!NAME & vbCr & _
                    "Student ID : " & rs!STUDENT_ID & vbCr & _
                    "We would like to remind you that the books you are borrowing as listed below, will be due." & vbCr & _
                    "Title : " & rs!TITLE & vbCr & _
                    "Due Date : " & rs!DUEDATE & vbCr & _
                    "We appreciate if you can return the book(s) as soon as possible." & vbCr & vbCr & _
                    "Best Regards," & vbCr & _
                    "LIBRARY IN NEW YORK"
            .Send
            rs.Edit
            rs!dateemailsent = Date
            rs.Update
        End With
        Set oEmailItem = Nothing
        Set oOutlook = Nothing
        rs.MoveNext
    End If
    Loop
Else
End If
rs.Close
Exit Sub:
    Exit Sub
End Sub
         

But, it gives me error enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source