I have a query already created in my database which pulls from 3 different tables PRF, Employee & Vendors.
When i try to run the Loop below it gives me an error on the 2nd .MoveNext and says there are no current records. Which I understand but the problem is that it leaves the last record out of the email being sent all other invoices in the query are included in the emails. What needs to be done to include that last record? - Public Sub SendMail1()
-
Dim dbs As DAO.Database
-
Dim rst As DAO.Recordset
-
Dim strSubject As String
-
Dim strEmailAddress As String
-
Dim strEMailMsg As String
-
Dim ingCounter As Integer
-
Dim intCount As Integer
-
Dim aOutlook As Object
-
Dim aEmail As Object
-
Dim mySQL As String
-
-
-
Set dbs = CurrentDb
-
Set rst = dbs.OpenRecordset("Email")
-
'Count of unsent e-mails
-
intCount = DCount("[ID]", "[PRF]" _
-
, "[Notified]=0")
-
'If count of unsent e-mails is zero then the procedure will not run
-
'If count of unsent e-mails is greater than zero, msgbox will prompt
-
'to send mail.
-
If intCount = 0 Then
-
MsgBox ("You have " & intCount & " emails to send.") _
-
, vbInformation, "Posted PRF"
-
Exit Sub
-
Else
-
-
rst.MoveFirst
-
Do While rst.EOF = False
-
'If rst.EOF = True Then Exit Do
-
Set aOutlook = CreateObject("Outlook.Application")
-
Set aEmail = aOutlook.CreateItem(0)
-
strEmailAddress = rst![EmployeeEmail]
-
aEmail.To = strEmailAddress
-
Do While rst![EmployeeEmail] = strEmailAddress
-
With aEmail
-
.Display
-
End With
-
signature = aEmail.Body
-
aEmail.Subject = "Posted payment Request"
-
aEmail.Body = "Invoice Number: " & rst![Invoice Number] & "" & " - " & "Vendor Name: " & rst![Vendor Name] & _
-
vbNewLine & signature
-
rst.MoveNext
-
If rst.EOF Then Exit Do
-
Loop
-
aEmail.Send
-
'If rst.EOF Then Exit Do
-
rst.MoveNext
-
-
Loop
-
End If
-
-
rst.Close
-
Set rst = Nothing
-
dbs.Close
-
Set dbs = Nothing
-
'Run update to update the sent mail check box
-
DoCmd.SetWarnings False
-
DoCmd.RunSQL "UPDATE PRF SET PRF.Notified = -1 WHERE (((PRF.Notified)=0))"
-
DoCmd.SetWarnings True
-
MsgBox "All new emails have been sent for posted PRF", vbInformation, "Thank You"
-
End Sub
6 1154 jforbes 1,107
Recognized Expert Top Contributor
I would try something like the following instead: - Do While Not rst.EOF
-
If rst![EmployeeEmail] = strEmailAddress Then
-
aEmail.Display
-
signature = aEmail.Body
-
aEmail.Subject = "Posted payment Request"
-
aEmail.Body = "Invoice Number: " & rst![Invoice Number] & "" & " - " & "Vendor Name: " & rst![Vendor Name] & vbNewLine & signature
-
End If
-
rst.MoveNext
-
Loop
This way, there is only one way out of the Loop, after everything has been accomplished.
That reads all the records but puts each invoice in an individual email instead of one per Employee with multiple invoice lines
jforbes 1,107
Recognized Expert Top Contributor
What does your code look like now?
- Set dbs = CurrentDb
-
Set rst = dbs.OpenRecordset("Email")
-
'Count of unsent e-mails
-
intCount = DCount("[ID]", "[PRF]" _
-
, "[Notified]=0")
-
'If count of unsent e-mails is zero then the procedure will not run
-
'If count of unsent e-mails is greater than zero, msgbox will prompt
-
'to send mail.
-
If intCount = 0 Then
-
MsgBox ("You have " & intCount & " emails to send.") _
-
, vbInformation, "Posted PRF"
-
Exit Sub
-
Else
-
-
rst.MoveFirst
-
Do While rst.EOF = False
-
-
Set aOutlook = CreateObject("Outlook.Application")
-
Set aEmail = aOutlook.CreateItem(0)
-
strEmailAddress = rst![EmployeeEmail]
-
aEmail.To = strEmailAddress
-
If rst![EmployeeEmail] = strEmailAddress Then
-
aEmail.Display
-
signature = aEmail.Body
-
aEmail.Subject = "Posted payment Request"
-
aEmail.Body = "Invoice Number: " & rst![Invoice Number] & "" & " - " & "Vendor Name: " & rst![Vendor Name] & _
-
vbNewLine & signature
-
End If
-
If rst.EOF Then Exit Do
-
'Loop
-
aEmail.Send
-
rst.MoveNext
-
Loop
-
End If
-
rst.Close
-
Set rst = Nothing
-
dbs.Close
-
Set dbs = Nothing
- Set dbs = CurrentDb
-
Set rst = dbs.OpenRecordset("Email")
-
'Count of unsent e-mails
-
intCount = DCount("[ID]", "[PRF]" _
-
, "[Notified]=0")
-
'If count of unsent e-mails is zero then the procedure will not run
-
'If count of unsent e-mails is greater than zero, msgbox will prompt
-
'to send mail.
-
If intCount = 0 Then
-
MsgBox ("You have " & intCount & " emails to send.") _
-
, vbInformation, "Posted PRF"
-
Exit Sub
-
Else
-
-
rst.MoveFirst
-
Do While rst.EOF = False
-
-
Set aOutlook = CreateObject("Outlook.Application")
-
Set aEmail = aOutlook.CreateItem(0)
-
strEmailAddress = rst![EmployeeEmail]
-
aEmail.To = strEmailAddress
-
If rst![EmployeeEmail] = strEmailAddress Then
-
aEmail.Display
-
signature = aEmail.Body
-
aEmail.Subject = "Posted payment Request"
-
aEmail.Body = "Invoice Number: " & rst![Invoice Number] & "" & " - " & "Vendor Name: " & rst![Vendor Name] & _
-
vbNewLine & signature
-
End If
-
If rst.EOF Then Exit Do
-
'Loop
-
aEmail.Send
-
rst.MoveNext
-
Loop
-
End If
-
rst.Close
-
Set rst = Nothing
-
dbs.Close
-
Set dbs = Nothing
jforbes 1,107
Recognized Expert Top Contributor
It's dangerous to rewrite code to this extent for a post here on Bytes because people will then expect you to fix it for them forever, like there is some kind of transfer of ownership of the code just by providing help. But I hope that doesn't happen here.
The following code is an example of how it could be structured so that it executes in a more predictable manner. I've put in a some comments to explain why there is a change or what the section is supposed to accomplish. Have a look and see if the changes make sense to you.
One last disclaimer, I didn't attempt to run the code, so there may be bugs or typos. - Public Sub SendMail1()
-
-
Dim dbs As DAO.Database
-
Dim rst As DAO.Recordset
-
Dim strSubject As String
-
Dim strEmailAddress As String
-
Dim strEMailMsg As String
-
Dim intCount As Integer
-
Dim aOutlook As Object
-
Dim aEmail As Object
-
Dim sSQL As String
-
Dim bFirstTime As Boolean
-
-
'Count of unsent e-mails
-
intCount = DCount("[ID]", "[PRF]", "[Notified]=0")
-
-
If intCount = 0 Then
-
'If count of unsent e-mails is zero then the procedure will not run
-
MsgBox ("You have " & intCount & " emails to send."), vbInformation, "Posted PRF"
-
Exit Sub
-
Else
-
'If count of unsent e-mails is greater than zero, msgbox will prompt to send mail.
-
If vbYes = MsgBox("You have " & intCount & " emails to send, continue?", vbQuestion, "Posted PRF") Then
-
-
' Setup email loop
-
bFirstTime = True ' Don't attempt to send an email before it's ready.
-
Set aOutlook = CreateObject("Outlook.Application") ' Only need to do this once
-
sSQL = "SELECT * FROM Email ORDER BY EmployeeEmail, [Vendor Name]"
-
Set dbs = CurrentDb
-
Set rst = dbs.OpenRecordset(sSQL)
-
-
' Loop through Invoices
-
If Not rst.EOF Then
-
Do While Not rst.EOF
-
If strEmailAddress <> rst![EmployeeEmail] Then
-
-
' EmployeeEmail has changed,
-
' So send off the current Email (if there is one)
-
' and create a New one.
-
If Not bFirstTime Then
-
' Send off previously created Email
-
aEmail.Body = strEMailMsg
-
aEmail.Send
-
Else
-
bFirstTime = False
-
End If
-
-
' Create new Email
-
strEmailAddress = rst![EmployeeEmail]
-
Set aEmail = aOutlook.CreateItem(0)
-
aEmail.To = strEmailAddress
-
aEmail.Subject = "Posted payment Request"
-
aEmail.Display
-
End If
-
-
' Add Invoice to current Email
-
strEMailMsg = strEMailMsg & "Invoice Number: " & rst![Invoice Number] & "" & " - " & "Vendor Name: " & rst![Vendor Name] & vbNewLine
-
rst.MoveNext
-
Loop
-
If Not bFirstTime Then
-
' Send Last email, since this hasn't been done yet.
-
aEmail.Body = strEMailMsg
-
aEmail.Send
-
End If
-
End If
-
-
' Cleanup. Don't worry about the DB Reference
-
rst.Close
-
Set rst = Nothing
-
-
'Run update to update the sent mail check box
-
sSQL = "UPDATE PRF SET PRF.Notified = -1 WHERE (((PRF.Notified)=0))"
-
dbs.Execute sSQL
-
MsgBox "All new emails have been sent for posted PRF", vbInformation, "Thank You"
-
-
End If
-
End If
-
End Sub
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: vasilijepetkovic |
last post by:
Hello All,
I have a problem with the program that should generate x number of txt
files (x is the number of records in the file datafile.txt).
Once I execute the program (see below) only one...
|
by: dixie |
last post by:
If I have a query that returns say, 3 records sorted by date, so that the
latest record is at the bottom of the list, can I then use just the
information from that last record to put onto an...
|
by: zpq |
last post by:
.....I need to loop through a record set and get the value in one
record and compare the value to the value in another record.
tia
stan
|
by: Mark |
last post by:
I'm using ASP.Net to accress a database, what I need to do is get the fields
out of the very last record in the db.
How do I do this?
Actually I'm after the primary key, titled 'AdID' it'll tell...
|
by: Rico |
last post by:
Hi All,
Just wondering, in vb code, how to if the last record on a cascading form is
the current record?
Thanks!
| |
by: Bhujanga |
last post by:
I want to scroll through records on an open form and take certain actions based on various criteria. So I want to have a loop such as this:
Do While Not Last Record <----- ?
....actions....
...
|
by: anfetienne |
last post by:
hi im back again.......i have a code to create strings and save it within a text file to pass variables to flash. im using the string format below.
...
|
by: LeighW |
last post by:
Hello,
I used Allen Browne's method of assigning default values from the last record which is especially helpful when adding a new record to filtered records. Using his method I had to create a...
|
by: Lynch225 |
last post by:
Hello all,
I have an issue with a form updating all records in a table except for the very last record for some reason.
A little background, I have an unmatched query that appends results to a...
|
by: RadioWriter |
last post by:
I'm working on building a database in Access 2013, and I'm coming across an error in a subroutine which checks to see if there are duplicate entries in the field PartNumbers (and marks a True/False...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |