Concatenate Field of Emails (1 Viewer)

randallst

Registered User.
Local time
Today, 17:46
Joined
Jan 28, 2015
Messages
64
Hi Everybody,

Me again, im hoping this is my last question as I have been playing all day and lost the plot haha!

I have created a Query that lists all email addresses that need to receive an email notification when we raise an incident in the database.

The query is called Qry_NotificationEmail and the field that I want to concatenate is called Email.

I currently have vba code linked to a button to generate a pre-populated email, but i have to manually enter the email addresses either into Outlook or the vba. I want it to be able to pull from the query in the event I move on / leave etc.

We are using Outlook 365 using the Desktop Application.

Code:
Private Sub Email_Notification_Button_Click()

Dim ccto As String
Dim bccto As String
Dim mailto As String

'Mail Recipients
mailto = ""
ccto = ""
bccto = ""

'Email Subject
mailsub = "New Incident: " & Me.Incident_ID & ""

'Email Message
emailmsg = "Incident ID: " & Me.Incident_ID & vbNewLine & "Category: " & Me.Category & vbNewLine & "Customer: " & Me.Customer & vbNewLine & "Title: " & Me.Title & vbNewLine & vbNewLine & "Please liaise with the Engineering Team for further information in relation to the above incident"

On Error Resume Next
DoCmd.SendObject acSendNoObject, , , mailto, ccto, bccto, mailsub, emailmsg, True

End Sub

Any help / guidance as always is really appreciated :)

All the best
Stuart

P.S. If anyone fancies telling me how to add a light blue background to the above code, please feel free ha-ha. It's a nice to have that I will play with in the future.
 

plog

Banishment Pending
Local time
Today, 11:46
Joined
May 11, 2011
Messages
11,646
how to add a light blue background

Emails are formatted using HTML. Every method I've seen says the best way to do this is with a table and using css to style it with the appropriate background:

 

Micron

AWF VIP
Local time
Today, 12:46
Joined
Oct 20, 2018
Messages
3,478
Well, beat to the punch again. Seeing as how I have this air code sitting on a Notepad doc, I might as well post it as much as just delete it.
Code:
Dim rs As DAO.Recordset

Set rs = Currentdb.Openrecordset("Qry_NotificationEmail")
If Not (rs.EOF And rs.BOF) Then
  rs.MoveFirst
  Do While Not rs.EOF
    If mailto = "" Then
      mailto = rs.Fields("Email")
    Else
      mailto = mailto & ";" & rs.Fields("Email")
    End If
    rs.MoveNext
  Loop
End If
Always step through such code at first lest you end up sending a ton of emails because you get stuck in a code loop. In fact, in the past I have gotten the record count and used a counter value to ensure no more emails can be sent than the number of address records - even if it means they all get sent to the same person!
 

Isaac

Lifelong Learner
Local time
Today, 09:46
Joined
Mar 14, 2017
Messages
8,777
For the light blue background, use the advice already given in this thread
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:46
Joined
Sep 21, 2011
Messages
14,270
FWIW I use
Code:
mailto = mailto &  rs.Fields("Email")  & ";"
Having the last character as ; does not cause any problems, and saves a little code and logic?
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 17:46
Joined
Jul 9, 2003
Messages
16,280
My code is similar to Micron's and is available on my website here:-


Along with a video explanation.

You may find this information on sending multiple emails from AWF contributor Gina Whipp helpful:-

 

Micron

AWF VIP
Local time
Today, 12:46
Joined
Oct 20, 2018
Messages
3,478
Having the last character as ; does not cause any problems,
I wasn't sure about that and didn't want to start with appending ; so I went the other way. Your advantage is that you've probably actually used this more or less recently instead of dreaming up air code. ;)
I used CDO when I sent mail and don't recall how I treated the recipients list - it was long ago.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 11:46
Joined
Feb 28, 2001
Messages
27,175
I've always used a short recordset loop and just concatenated the e-mail addresses with a semi-colon as a separator.
 

Users who are viewing this thread

Top Bottom