VBA Access get string from db

懵懂的女人 提交于 2019-12-23 03:25:33

问题


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

me@email.com;me2@email.com;me3@email.com...etc

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


回答1:


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


来源:https://stackoverflow.com/questions/16510800/vba-access-get-string-from-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!