简体   繁体   中英

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.

This mails exist in a field "MailField" from a Query "Query1". So I'm trying to loop into all rows and send mails using the "MailField" with the corresponding value of row i as "Message":

Example: mail to "mail2@123.com" With a message "value of field a in row 2".

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 hope you can help me.

Thank you.

To use a value from a query in VBA, don't use 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM