combine multiple email addresses into .bcc from recordset to send outlook email from access vba

adhoustonj

Member
Local time
Today, 08:44
Joined
Sep 23, 2022
Messages
192
Hello,
I hope this post is in the right place. The code below is populating the bcc and to fields in my outlook message. Is it possible to only populate the bcc and leave the to field blank? Or am I approaching this wrong and should loop through and send an individual email to each email from the recordset?





Code:
Private Sub Command180_Click()
    Dim rs As DAO.Recordset
    Dim OlApp As Object
    Dim OutMail As Object
    Dim strEmail As String
    Dim bccEmails As String


    Set OlApp = CreateObject("Outlook.Application")
    Set OutMail = OlApp.CreateItem(olMailItem)


    Set rs = CurrentDb.OpenRecordset("SELECT DISTINCT Email FROM tblEmail")
    With rs
        Do Until .EOF
            strEmail = !Email
            ' add email to BCC email list string
            bccEmails = bccEmails & strEmail & ";"


            With OutMail
                .Recipients.Add strEmail
            End With


            rs.MoveNext
        Loop
    End With
    rs.Close
    Set rs = Nothing


    ' set BCC using string of concatenated emails                 
    OutMail.BCC = left(bccEmails, Len(bccEmails) - 1) ' trims trailing semicolon


    OutMail.Display


End Sub
 
Never mind... I see what was happening. the .recipients.Add strEmail was adding them to the .to and then outMail.BCC is being set with bccEmails
 
Concat is shorter when using an ADODB.Recordset.

Code:
' set BCC using string of concatenated emails                 
 OutMail.BCC = ADODBRecordset.GetString(adClipString, , , ";")
 
Hello,
I hope this post is in the right place. The code below is populating the bcc and to fields in my outlook message. Is it possible to only populate the bcc and leave the to field blank? Or am I approaching this wrong and should loop through and send an individual email to each email from the recordset?





Code:
Private Sub Command180_Click()
    Dim rs As DAO.Recordset
    Dim OlApp As Object
    Dim OutMail As Object
    Dim strEmail As String
    Dim bccEmails As String


    Set OlApp = CreateObject("Outlook.Application")
    Set OutMail = OlApp.CreateItem(olMailItem)


    Set rs = CurrentDb.OpenRecordset("SELECT DISTINCT Email FROM tblEmail")
    With rs
        Do Until .EOF
            strEmail = !Email
            ' add email to BCC email list string
            bccEmails = bccEmails & strEmail & ";"


            With OutMail
                .Recipients.Add strEmail
            End With


            rs.MoveNext
        Loop
    End With
    rs.Close
    Set rs = Nothing


    ' set BCC using string of concatenated emails                
    OutMail.BCC = left(bccEmails, Len(bccEmails) - 1) ' trims trailing semicolon


    OutMail.Display


End Sub
You really should be checking for eof before trying to use a recordset?
 
I just check for EOF. If the recordset is empty when you open it, that is True as is BOF, however I just use the one test, EOF.
 
I just check for EOF. If the recordset is empty when you open it, that is True as is BOF, however I just use the one test, EOF.
so would you use "if not rs.eof then"? Thanks!
 
Yes, but if you are looping, I would use Do Until rst.EOF
 

Users who are viewing this thread

Back
Top Bottom