I am getting an error "Loop without Do". Any help would be appreciated.
Private Sub Command14_Click()
Me.Refresh
Me.Requery
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim sql As String
Set db = CurrentDb
Set rs = db.OpenRecordset("qrySalespersonUpdate")
sql = "SELECT tblSamples.[intDNRSample#], tblSamples.ynColifUnsafe, tblSamples.ynEcolSafe, tblSamples.ynEcolUnsafe, tblSamples.ynHighNitrates, tblSamples.txtWellAddress, tblSamples.datSampleDate, tblSamples.Salesperson, tblSamples.datReported, tblSamples.txtCounty, tblSamples.txtTownship FROM tblSamples LEFT JOIN tblSalespersonEmail ON tblSamples.Salesperson = tblSalespersonEmail.Salesperson WHERE (((tblSamples.ynColifUnsafe) = -1) And ((tblSamples.Salesperson) Is Not Null)) And ((tblSamples.datReported) = #" & [Forms]![frmReportPopUp]![rdate] & "#))Or (((tblSamples.ynHighNitrates) = -1) And ((tblSamples.Salesperson) Is Not Null)) And ((tblSamples.datReported) = #" & [Forms]![frmReportPopUp]![rdate] & "#))"
Set rs = db.OpenRecordset(sql, dbOpenSnapshot)
If rs.BOF And rst.EOF Then
Call MsgBox("No Results Need to be Emailed")
Else
Dim strEmail, strBody As String
Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem
'**creates an instance of Outlook
Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)
Do While Not rs.EOF
rs.MoveFirst
strEmail = txtEmail
strBody = "Unique Well # " & rs("intDNRSample#") & Chr(13)
strBody = strBody & " " & Chr(13)
strBody = strBody & "County: " & rs("txtCounty") & Chr(13)
strBody = strBody & "Township: " & rs("txttownship") & Chr(13)
strBody = strBody & "Address: " & rs("txtWellAddress") & Chr(13)
strBody = strBody & " " & Chr(13)
strBody = strBody & "Bacti: " & rs("Bact") & Chr(13)
strBody = strBody & "Nitrate: " & rs("intNitrate")
'***creates and sends email
With objEmail
.To = rs("Email")
.CC = "sharonp@samswelldrilling.com"
.Subject = "Unsafe Water and/or High Nitrate Results "
.Body = strBody
.Display
rs.MoveNext
Loop
rs.Close
db.Close
'****closes Outlook. remove if you do not want to close Outlook
objOutlook.Quit
End With
End Sub
Private Sub Command14_Click()
Me.Refresh
Me.Requery
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim sql As String
Set db = CurrentDb
Set rs = db.OpenRecordset("qrySalespersonUpdate")
sql = "SELECT tblSamples.[intDNRSample#], tblSamples.ynColifUnsafe, tblSamples.ynEcolSafe, tblSamples.ynEcolUnsafe, tblSamples.ynHighNitrates, tblSamples.txtWellAddress, tblSamples.datSampleDate, tblSamples.Salesperson, tblSamples.datReported, tblSamples.txtCounty, tblSamples.txtTownship FROM tblSamples LEFT JOIN tblSalespersonEmail ON tblSamples.Salesperson = tblSalespersonEmail.Salesperson WHERE (((tblSamples.ynColifUnsafe) = -1) And ((tblSamples.Salesperson) Is Not Null)) And ((tblSamples.datReported) = #" & [Forms]![frmReportPopUp]![rdate] & "#))Or (((tblSamples.ynHighNitrates) = -1) And ((tblSamples.Salesperson) Is Not Null)) And ((tblSamples.datReported) = #" & [Forms]![frmReportPopUp]![rdate] & "#))"
Set rs = db.OpenRecordset(sql, dbOpenSnapshot)
If rs.BOF And rst.EOF Then
Call MsgBox("No Results Need to be Emailed")
Else
Dim strEmail, strBody As String
Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem
'**creates an instance of Outlook
Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)
Do While Not rs.EOF
rs.MoveFirst
strEmail = txtEmail
strBody = "Unique Well # " & rs("intDNRSample#") & Chr(13)
strBody = strBody & " " & Chr(13)
strBody = strBody & "County: " & rs("txtCounty") & Chr(13)
strBody = strBody & "Township: " & rs("txttownship") & Chr(13)
strBody = strBody & "Address: " & rs("txtWellAddress") & Chr(13)
strBody = strBody & " " & Chr(13)
strBody = strBody & "Bacti: " & rs("Bact") & Chr(13)
strBody = strBody & "Nitrate: " & rs("intNitrate")
'***creates and sends email
With objEmail
.To = rs("Email")
.CC = "sharonp@samswelldrilling.com"
.Subject = "Unsafe Water and/or High Nitrate Results "
.Body = strBody
.Display
rs.MoveNext
Loop
rs.Close
db.Close
'****closes Outlook. remove if you do not want to close Outlook
objOutlook.Quit
End With
End Sub