简体   繁体   English

如何使用访问查询到VBA代码中的字段值

[英]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.

相关问题 如何在vba访问表单中设置文本框的值来查询字段值 - How to set the value of textbox in vba access form to query field value 从访问表单到 excel 的 1 个字段的 Vba 代码 - Vba code for 1 field from access form to excel 如何使用VBA从Access模糊查询中获取结果? - How to use VBA to Get result from an Access fuzzy query? 访问VBA:如何订阅控件/字段的更改是由其他vba代码而不是用户交互引起的 - Access VBA: how to subscribe to a the change of a control/field happened from other vba code and not from user interaction 可以编写 Access VBA 代码以从查询和表单字段插入到表中吗? - Can Access VBA code be written to insert into a table from both a Query and a Form field? 在Access VBA中从具有唯一编号的另一个字段值创建串联代码 - Creating a Concatenated Code from another field value with unique number In Access VBA 使用 Microsoft Access 2010 中的 VBA 代码将表字段中的值获取到变量中 - Get a value from a table field into a variable using VBA code in Microsoft Access 2010 如何在Access查询中使用VBA创建的SQL - how to use VBA created SQL in an Access query 如何通过ms access中的VBA代码更新任何字段值? - How can I update any field value through VBA code in ms access? 如何使用此 VBA 代码在 Access VBA 中用作公共子程序 - How to use this VBA code to use as Public Subroutine in Access VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM