Question : Access Automatically send email once payment amount is due

Hello Experts,

I am trying to create a query where once a payment amount due after 30 days, then it will automatically send an email to me.

Currently, I have a query for 'Amount Due' and 'Workorder' and 'Invoice date'

How can I set if Amount Due > 0 and after 30 days of invoice date, then it will automatically send an email to me?

Should I create a query? macro or module? I want it to be automatically detect without opening any form. or may be form on_load on the form I usually open?

If this is confusing, I will try to explain clearer.

Thanks

Answer : Access Automatically send email once payment amount is due

> How can I set if Amount Due > 0 and after 30 days of invoice date
add >0 to the amountdue column and <(DateAdd("d",Date(),-30)) to the invoice date column in your query.  
            WHERE (((AmountDue)>0) AND ((DateDue)<(DateAdd("d",Date(),-30))))

To send the email.. you could run the code whenever you open the database, when you open a specific form, when you click a button or even call it from a macro and use a scheduled task to send it at a specific time. Depends on how you want to do it.

For the code.. here is a simple example.. modifying some of the code others have posted. Paste these two functions into a module in your database and edit the SendReport function changing "YourSavedQueryName" to the actual name of your query and "you@somewhere.com" to your email address as well as anything else (such as field names) that needs changed. Then call the SendReport function from wherever you would like (like on a button click or from a macro) to send the report.

Public Function OutlookSend(ByVal MailUser As String, ByVal MsgSubject As String, ByVal msgbody As String) As Boolean
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
On Error GoTo ErrHandler

If Not IsNull(MailUser) And MailUser <> "" Then

Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
Recipient = MailUser
With MailOutLook
.To = Recipient
.Subject = MsgSubject
.Body = msgbody
.DeleteAfterSubmit = True 'This would let Outlook send the note without storing it in your sent bin
.Send
End With

Set MailOutLook = Nothing
Set appOutLook = Nothing

End If 'Not IsNull(MailUser)

ExitHere:
Exit Function

ErrHandler:
  Select Case Err.Number
Case 0
   
    Resume ExitHere

Case Else

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in  OutlookSend"

End Select
End Function





Public Function SendReport()

   
    Dim MyMsgBody, MySubject, MyMailAddress As String
    Dim ItemCount As Integer
    On Error GoTo ErrHandler

If DCount("*", "YourSavedQueryName") > 0 Then


Dim dbs As Database, rst As DAO.Recordset

    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset("YourSavedQueryName")

rst.MoveFirst
ItemCount = 0
Do Until rst.EOF
item1 = rst!AmountDue
item2 = rst!WorkOrder
item3 = rst![Invoice Date]
MyMsgBody = MyMsgBody & item1 & vbTab & item2 & vbTab & item3 & vbCrLf
ItemCount = ItemCount + 1
rst.MoveNext
Loop
rst.Close

MyMailAddress = "you@somewhere.com"
MySubject = "Invoices Due Report: " & ItemCount & " Items"

If Not IsNull(MyMsgBody) And MyMsgBody <> "" Then Ret = OutlookSend(MyMailAddress, MySubject, MyMsgBody)

End If


ExitHere:
Exit Function

ErrHandler:
  Select Case Err.Number
Case 0
   
    Resume ExitHere

Case Else

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in  SendReport"

End Select
End Function
Random Solutions  
 
programming4us programming4us