Access Macro / VBA Question

Mac M
Mac M used Ask the Experts™
I have a Email macro which I converted to VB in Access 2016. When you click the Email button for each record the macro attaches the detail spreadsheet and email the client. I have to click this email button a total of 550 times or more for each client records...My form has two parts to it...I click on each Payee record to pull the details form up and then I click on the Email Detail button to email the client with their attached detail pay spreadsheet. I would like the Email button to loop through each record and perform the task without having to manually go through each record  until EOF but I'm unsure how and where to place this in my macro or code...Any help with this would be greatly appreciated...Thanks in advance...See the sample files below...
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You could put the email code in a while - wend loop.

The code below just prints out 20 when it's done. Your code would go in the while section and assumes you know the number of client records (550+). If you have the clients in a table it should be easy enough to determine the number.

If you need to count records of the client maybe try DCount to return the number of records and let that be your end(i.e., replace the 20 in the sample code).

Dim Counter 
Counter = 0 ' Initialize variable. 
While Counter < 20 ' Test value of Counter. 
 Counter = Counter + 1 ' Increment Counter. 
Wend ' End While loop when Counter > 19. 
Debug.Print Counter ' Prints 20 in the Immediate window. 

Open in new window

Software & Systems Engineer
Function EmailPymtDetail()
On Error GoTo EmailPymtDetail_Err
Dim rst as DAO.RecordSet
'Here i assume that you have a table named clients that has this structure
' ClientID , ClientName,ClientEmail
with rst
While Not .EOF
DoCmd.SendObject acQuery, "CurrentDetail", "Excel97-Excel2003Workbook(*.xls)", .Fields("ClientEmail"), "", "", "Bill Cleaning Services: Payment Detail", "Greetings.  Please find the attached payment detail for the payment released today from the Cleaning Custodian.  If you have any questions or would like additional information please contact"
end With    

    Exit Function

    MsgBox Error$
    Resume EmailPymtDetail_Exit

End Function

Open in new window

Scott McDaniel (EE MVE )Infotrakker Software
Most Valuable Expert 2012
Top Expert 2014

Note that running Sendobject in a loop has been known to hang with longer running processes. You'd be much better off automating your email client to do this instead of using SendObject. You can't do that with a macro, however. You'd have to use VBA.
Mac MSolutions Engineer


I'm using John's solution and working with him to simply my efforts...Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial