VBA Access从数据库获取字符串 [英] VBA Access get string from db

查看:78
本文介绍了VBA Access从数据库获取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,其中的一列包含电子邮件地址.

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?

推荐答案

从内存中(未测试):

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

这篇关于VBA Access从数据库获取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆