Hi snehal0909
To expand on Dennis' comment (btw Dennis, thank you for the assist)
Dennis' step1. to select which invoices to send:
This is the query SQL which looks for the selected invoices
tblInvoices.SendInvoice = -1It also checks they have a valid invoice address
Len(tblClients.Email & '') >0"strSQL = "SELECT DISTINCT tblClients.Email, tblInvoices.InvoiceID, tblInvoices.InvoiceNo, tblInvoices.SendInvoice " & _
"FROM tblClients INNER JOIN tblInvoices ON tblClients.ClientID = tblInvoices.ClientID " & _
"WHERE tblInvoices.SendInvoice = -1 AND Len(tblClients.Email & '') >0"
The SQL joins the list of clients (tblClients) with the table of invoices (tblInvoices) using ClientID as the field relating the two tables.
Step 2 is to open the query and loop through each record:
CODE
Set rst = CurrentDB.OpenRecordSet(strSQL)
While rst.EOF = False
Debug.Print rst!InvoiceNo
rst.MoveNext
Wend
set rst = Nothing
This will loop through the records returned by the query recordset and print the invoice number to the immediate window.
Step 3 Create the Strings for the addressee, title and body of the email
CODE
strSubject = "Invoice No: " & rst!InvoiceNo
strtext = "You invoice " & rst!InvoiceNo & " is attached" & "whatever other text you want"
strEMail = rst!tblClients.Email
Debug.Print strSubject
Debug.Print strtext
Debug.Print strEMail
Step 4 open the report
DoCmd.OpenReport "ReportName", acViewPreview, , "InvoiceID=" & rst!tblInvoices.InvoiceID
Do this for one report only - Comment out (temporarily) the While ... Wend comands to give
CODE
Set rst = CurrentDB.OpenRecordSet(strSQL)
'While rst.EOF = False
strSubject = "Invoice No: " & rst!InvoiceNo
strtext = "You invoice " & rst!InvoiceNo & " is attached" & "whatever other text you want"
strEMail = rst!tblClients.Email
DoCmd.OpenReport "ReportName", acViewPreview, , "InvoiceID=" & rst!tblInvoices.InvoiceID
' rst.MoveNext
'Wend
set rst = Nothing
Step 5 Send the e-mail for the 1 report
by adding
DoCmd.SendObject acReport, "ReportName", acFormatPDF, strEMail, , , strSubject, strtext, true
after the DoCmd.OpenReport line, note the 'True' statement at the end of the line
Step 6
Close the report after the send Object , and re-enable the loop (by removing the comment marks), and move through the recordset with the movenext command
CODE
...
While rst.EOF = False
strSubject = "Invoice No: " & rst!InvoiceNo
strtext = "You invoice " & rst!InvoiceNo & " is attached" & "whatever other text you want"
strEMail = rst!tblClients.Email
DoCmd.OpenReport "ReportName", acViewPreview, , "InvoiceID=" & rst!tblInvoices.InvoiceID, acHidden
DoCmd.SendObject acReport, "ReportName", acFormatPDF, strEMail, , , strSubject, strtext, False
DoCmd.Close acReport, "ReportName"
' rst.Edit
' rst!SendInvoice = 0 ' Clear send invoice flag to indicate invoice sent
' rst.Update
rst.MoveNext
Wend
set rst = Nothing
Note
The Open Report has 'acHidden' to prevent the report flashing up each time a new record is processed,
The Sendobject command has 'False' at the end to send the e-mail without opening it for user edit first.
rst.Edit ... rstUpdate is commented out , the code sets the SendInvoice flag to 0 and hence prevents the code resending the invoice the next time it is run, - you don't want the code to do this while you are testing.
Finally step 7
Enable the rst.Edit ... rst.Update code to clear the SendInvoice flag.
hth