Using Outlook to email

J

JD

I have attached code below for my problem.

I am attempting to email as many as four separate reports
to an multiple email recipients using [tbl_ToBeMailed].
The procedure reads in each record as a recordset, and
depending on True/False values, loops until end of file.

My problem is that the loop, which worked once, will now
1. Populate the output file
2. Send the first report for the first recordset

Do I need to reset a variable in the loop?

Thanks in advance for any assistance

Option Compare Database
Option Explicit

Public Function Generate_Surveys()

Dim prst ' As Recordset
Dim scrst As Recordset
Dim output ' as recordset
Dim strSQL As String
Dim dVar As String


' Get record
strSQL = "SELECT * FROM tbl_TMailedtmp"
Set output = CurrentDb.OpenRecordset(strSQL)


' Get Record
strSQL = "SELECT * FROM tbl_ToBeMailed"
Set prst = CurrentDb.OpenRecordset(strSQL)

prst.MoveFirst

Do While Not prst.EOF

Rem

If prst.Fields(2) = -1 Then
DoCmd.SendObject acSendReport, "rpt1", ,
prst.Fields(6), , , "Message1", "Body1", False


With output
.AddNew
![ProjectNumber] = prst.Fields(0)
![employeeno] = prst.Fields(1)
![HEG_ID] = "HEG1"
![SurveyDate] = DateSerial(Year(Now
()), Month(Now()), Day(Now()))
.Update
End With
Else
End If

If prst.Fields(3) = -1 Then
DoCmd.SendObject acSendReport, "rpt2", ,
prst.Fields(6), , , "Message2", "Body2", False


With output
.AddNew
![ProjectNumber] = prst.Fields(0)
![employeeno] = prst.Fields(1)
![HEG_ID] = "HEG2"
![SurveyDate] = DateSerial(Year(Now
()), Month(Now()), Day(Now()))
.Update
End With

Else
End If

If prst.Fields(4) = -1 Then
DoCmd.SendObject acSendReport, "rpt3", ,
prst.Fields(6), , , "Message3", "Body3", False

With output
.AddNew
![ProjectNumber] = prst.Fields(0)
![employeeno] = prst.Fields(1)
![HEG_ID] = "HEG3"
![SurveyDate] = DateSerial(Year(Now
()), Month(Now()), Day(Now()))

.Update

End With

Else
End If

If prst.Fields(5) = -1 Then

DoCmd.SendObject acSendReport, "rpt4", ,
prst.Fields(6), , , "Message4", "Body4", False
With output
.AddNew
![ProjectNumber] = prst.Fields(0)
![employeeno] = prst.Fields(1)
![HEG_ID] = "HEG4"
![SurveyDate] = DateSerial(Year(Now
()), Month(Now()), Day(Now()))

.Update

End With

Else
End If

prst.MoveNext
Loop
prst.Close

End Function
 

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