Using query results in VB code

L

Lisa

How can I access query results from within VB? I would
like to loop through query results to send an email to
each user resulting in the query.

Thanks.
 
C

Cheryl Fischer

Lisa,

You can loop through your query by opening it as a RecordSet. Here is some
sample code for you - however, before you try to run it, be sure that you
have set a Reference to the appropriate Outlook Object Library.

Dim g_db as Database
Dim g_rs as DAO.Recordset
Dim oApp As Outlook.Application
Dim objNewMail As Outlook.MailItem

Set oApp = New Outlook.Application

Set g_db = CurrentDb
Set g_rs = g_db.OpenRecordset("MyQueryName", dbOpenSnapshot)

g_rs.MoveLast
g_rs.MoveFirst
Do While Not g_rs.EOF
Set objNewMail = oApp.CreateItem(olMailItem)
With objNewMail
.To = g_rs!MyEmailAddress
.Subject = "Put your subject here"
.Body = "Put your message here"
.Save
.Send
End With
g_rs.MoveNext
Loop
MsgBox "Finished sending emails!"

Set oApp=Nothing

g_rs.close
set g_rs=Nothing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top