Access 2013 VBA:查询收件人电子邮件地址

时间:2018-03-26 19:25:23

标签: sql vba access-vba

如何将以下SQL查询添加为Outlook电子邮件的收件人?

SELECT 
     E.Email
FROM 
     Employee E
          INNER JOIN ProposalTracking P ON E.Initials = P.UW

这是VBA代码,它只在Outlook中打开一封新电子邮件:

Private Sub btnEmail_Click()

Dim varName As Variant
Dim varSubject As Variant
Dim varBody As Variant

varName = "first.last@example.com"

varSubject = "TPS Report"

varBody = "Did you see the memo? We use the new cover sheets now."

DoCmd.SendObject , , , varName, , varSubject, varBody, True, False

End Sub

1 个答案:

答案 0 :(得分:1)

通过DAO实现这一目标的一种方式:

Dim email as string
Dim sqlSTR as string
Dim r As DAO.Recordset
Dim dbs as DAO.database
set dbs = currentDB()
sqlSTR = "SELECT E.Email FROM  Employee E INNER JOIN ProposalTracking P ON E.Initials = P.UW"

Set r = dbs.OpenRecordset(Name:=sqlSTR , Type:=dbOpenSnapshot)
varName = r![Email]
r.close

虽然,如果这样做,我会在where子句中添加变量。它看起来像这样:

Dim varSubject As Variant
Dim varBody As Variant
Dim email as string
Dim sqlSTR as string
Dim r As DAO.Recordset
Dim varInitials as string
Dim dbs as DAO.database

set dbs = currentDB()

varInitials = "P.UW"
sqlSTR = "SELECT E.Email FROM  Employee E INNER JOIN ProposalTracking P ON E.Initials = P.UW WHERE " & someField & " = & " & """" & someVariable & """"

Set r = dbs.OpenRecordset(Name:=sqlSTR , Type:=dbOpenSnapshot)
varName = r![Email]
r.close

varSubject = "TPS Report"

varBody = "Did you see the memo? We use the new cover sheets now."

DoCmd.SendObject , , , varName, , , varSubject, varBody, True, False

在sqlSTR中,将someField替换为您匹配的任何内容(例如,P.UW)和someVariable以及您希望匹配的某些值(例如" John")。如果它是一个int值,请删除& """"来自someVariable值