[英]how to use a field value from access query into vba code
Hi,I'm trying to make a Access macro, what sends mails to persons. 嗨,我正在尝试制作一个Access宏,它将邮件发送给人们。
This mails exist in a field "MailField" from a Query "Query1". 该邮件存在于查询“ Query1”的字段“ MailField”中。 So I'm trying to loop into all rows and send mails using the "MailField" with the corresponding value of row i as "Message": 因此,我尝试遍历所有行,并使用“ MailField”发送邮件,并将第i行的相应值作为“消息”:
Example: mail to "mail2@123.com" With a message "value of field a in row 2". 示例:邮件至“ mail2@123.com”,并显示消息“第2行中的字段a的值”。
Function makroabc()
On Error GoTo makroabc_Err
For i = "first Raw" To "mumber of data in the query"
If (Eval("Hour(Now()) Between 11 And 16")) Then
DoCmd.OpenQuery "Query1", acViewNormal, acReadOnly
DoCmd.SendObject , "", "", "Mail from row i", "", "", "Subject", "Message", False, ""
End If
Next i
makroabc_Exit:
Exit Function
makroabc_Err:
MsgBox Error$
Resume makroabc_Exit
End Function
What can i input instead of "Mail from row i" ? 我可以输入什么而不是“来自第i行的邮件”?
I hope you can help me. 我希望你能帮助我。
Thank you. 谢谢。
To use a value from a query in VBA, don't use DoCmd.OpenQuery
. 要在VBA中使用查询中的值,请不要使用DoCmd.OpenQuery
。 Open a Recordset instead. 而是打开一个记录集 。
Eg to send to all recipients in the query, loop over the recordset: 例如,发送给查询中的所有收件人,遍历记录集:
Dim db as DAO.Database
Dim rs as DAO.Recordset
Dim subj as String
Set db = CurrentDB
Set rs = db.OpenRecordset("Query1")
Do While Not rs.EOF
subj = "Hello " & rs![field b]
DoCmd.SendObject acSendNoObject, , , rs!Mailfield, , , subj, "Message", False
rs.MoveNext
Loop
rs.Close
Note though that Outlook may display a security warning. 请注意,尽管Outlook可能会显示安全警告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.