docmd send object sending all records to each person

M

Marcie

I have the following code to send an email to each recipient in the
payrolldetail table. I only want it to send there information but it is
sending everyones. Can anyone see what I am doing wrong?

Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("PayrollDetail")

rs.MoveFirst
While Not rs.EOF
strEmail = rs!Email

strid = rs!ID

DoCmd.OpenReport "ContractFacntp", acViewPreview, , strid

DoCmd.SendObject acSendReport, "ContractFACNTP", "PDFFormat(*.pdf)",
strEmail, , , "Contract"
DoCmd.Close acReport, "ContractFACNTP"

rs.MoveNext
Wend
rs.Close
Set rs = Nothing
 
J

John W. Vinson

I have the following code to send an email to each recipient in the
payrolldetail table. I only want it to send there information but it is
sending everyones. Can anyone see what I am doing wrong?

Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("PayrollDetail")

rs.MoveFirst
While Not rs.EOF
strEmail = rs!Email

strid = rs!ID

DoCmd.OpenReport "ContractFacntp", acViewPreview, , strid

DoCmd.SendObject acSendReport, "ContractFACNTP", "PDFFormat(*.pdf)",
strEmail, , , "Contract"
DoCmd.Close acReport, "ContractFACNTP"

rs.MoveNext
Wend
rs.Close
Set rs = Nothing

The problem is that the Report that it's sending is evidently based on the
entire table. You're passing strid in the WhereCondition argument; that's
defined in the help as

Optional Variant. A string expression that's a valid SQL WHERE clause without
the word WHERE

What you're passing is just a number - 3456 or whatever that record's ID is.
Access can't recognize that as a valid SQL WHERE clause; in fact I'm surprised
it isn't giving an error message!

Try

DoCmd.OpenReport "ContractFacntp", acViewPreview, , "ID = " & strid

to limit the report to that individual ID.
 

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