1

I have a table with a column contain email addresses.

I would like to get these email addresses and send mail to them. My problem is that i don't know how to get the addresses. I would like to get a string as

[email protected];[email protected];[email protected]

How can i get this string in order to pass it to the recipents?

3
  • have you tried using that string as it is? Commented May 12, 2013 at 18:44
  • what do you mean?i don't know the emails that will be in the db Commented May 12, 2013 at 18:44
  • Is that how the strings are in you DB? Commented May 12, 2013 at 18:58

1 Answer 1

2

From memory (not tested):

Dim db As DAO.Database, rs As DAO.Recordset
Dim s As String

Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT email FROM myTable WHERE ...")
Do While Not rs.EOF
    If s = "" Then
        s = rs!email
    Else
        s = s & ";" & rs!email
    End If
    rs.MoveNext
Loop
rs.Close: Set rs = Nothing
db.Close: Set db = Nothing

Since concatenating strings from a query is a frequent task, I suggest creating a reusable Function:

Public Function ConcatenateFromSql(ByVal sql As  String, _
                                   Optional ByVal delimiter As String = ";") As String
    Dim db As DAO.Database, rs As DAO.Recordset
    Dim s As String

    Set db = CurrentDb
    Set rs = db.OpenRecordset(sql)
    Do While Not rs.EOF
        If s = "" Then
            s = rs(0)
        Else
            s = s & delimiter & rs(0)
        End If
        rs.MoveNext
    Loop
    rs.Close: Set rs = Nothing
    db.Close: Set db = Nothing

    ConcatenateFromSql = s
End Function
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.