Not a problem, I do this kind of stuff fairly often.
If you were to take the DAO.Recordset approach I would simply split up the code. For readability and modularity, I like to write relatively basic functions and public subroutines in modules and then call them from my events along with the necessary specifications. If you take the code I referenced above, I would just include another DAO.Recordset that selects the email addresses of the people on the distribution list (another SQL statement should take care of that) and then add them to a String variable (I included distList in my code above but forgot to use it, sorry) and pass that in to your email subroutine as the email recipients. Something kinda like this (assuming this is added on to the code I first posted):
Code:
strSQL2 = "SELECT emailAddress FROM DistributionTable WHERE DistributionCity = '" & City & "';"
rst2 = CurrentDb.OpenRecordset(strSQL2)
Do Until rst.EOF
distList = distList & ";" & rst![emailAddress]
rst.MoveNext
Loop
' I like to remove the first semicolon the above loop will generate
distList = = Right(distList, Len(distList) - 1)
Call Emails(distList,emailText)
This assumes you have already coded a public subroutine that generates an email with the first variable passed in being used as the recipients and the second as the body text. I've included a sneaky little email code I have written into my Access db which automatically sends me error reports when an error is generated.
Code:
Sub ErrorEmail(ErrNum, ErrDesc, Object, R)
' The purpose of this subroutine is to send the database admin an email any time an error occurs.
' This email will contain the date/time the error occured along with the error number and description
' and the form/report being used when the error occured.
Dim objOL As Outlook.Application, objMsg As Outlook.MailItem, objRecip As Outlook.Recipient, V As String
V = "v2.8" ' @ UPDATE THIS WITH EACH VERSION @
Set objOL = CreateObject("Outlook.Application")
Set objMsg = objOL.CreateItem(olMailItem)
With objMsg
Set objRecip = .Recipients.Add("myemail@me.net")
objRecip.Type = olTo
.Subject = "Error Occured in myDb" & V & ": " & Err.Number & " - " & Err.Description
.Body = "An error occurred in the Access application myDb" & _
V & " at the following time: " & Now() & " with the following number and description. " & _
vbCr & ErrNum & ": " & ErrDesc & vbCr & vbCr & _
"The error occurred in the exectution of " & R & _
" while the user was using the " & Object & " object in SMART Trackers."
For Each objRecip In .Recipients
objRecip.Resolve
Next
.Send '<- If set to Send will send email automatically. If set to Display, will open the Outlook window and the user will have to press Send
End With
End Sub
As part of my error handling code I include a call to this sub. Example: Call ErrorEmail(Err.Number, Err.Description, Object, "Report_Open"). In this case I use the caption of the main report label to set the Object variable. This is an example of how you could write an email automation sub to accept different distribution lists (Set objRecip = .Recipients.Add(Your Variable Here) ) as well as the message text (.Body = Whatever you want). This way, if your users like this functionality, you can easily build it in to any number of events without having to re-code the entire thing.