473,507 Members | 2,395 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Loop not reading last record in Query

4 New Member
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?

Expand|Select|Wrap|Line Numbers
  1. Public Sub SendMail1()
  2.  Dim dbs As DAO.Database
  3. Dim rst As DAO.Recordset
  4. Dim strSubject As String
  5. Dim strEmailAddress As String
  6. Dim strEMailMsg As String
  7. Dim ingCounter As Integer
  8. Dim intCount As Integer
  9. Dim aOutlook As Object
  10. Dim aEmail As Object
  11. Dim mySQL As String
  12.  
  13.  
  14. Set dbs = CurrentDb
  15. Set rst = dbs.OpenRecordset("Email")
  16.  'Count of unsent e-mails
  17. intCount = DCount("[ID]", "[PRF]" _
  18. , "[Notified]=0")
  19. 'If count of unsent e-mails is zero then the procedure will not run
  20. 'If count of unsent e-mails is greater than zero, msgbox will prompt
  21. 'to send mail.
  22.      If intCount = 0 Then
  23.         MsgBox ("You have " & intCount & " emails to send.") _
  24.         , vbInformation, "Posted PRF"
  25.         Exit Sub
  26.     Else
  27.  
  28. rst.MoveFirst
  29. Do While rst.EOF = False
  30. 'If rst.EOF = True Then Exit Do
  31.     Set aOutlook = CreateObject("Outlook.Application")
  32.     Set aEmail = aOutlook.CreateItem(0)
  33.     strEmailAddress = rst![EmployeeEmail]
  34.     aEmail.To = strEmailAddress
  35.         Do While rst![EmployeeEmail] = strEmailAddress
  36.          With aEmail
  37.          .Display
  38.          End With
  39.          signature = aEmail.Body
  40.          aEmail.Subject = "Posted payment Request"
  41.          aEmail.Body = "Invoice Number: " & rst![Invoice Number] & "" & " - " & "Vendor Name: " & rst![Vendor Name] & _
  42.          vbNewLine & signature
  43.          rst.MoveNext
  44.          If rst.EOF Then Exit Do
  45.         Loop
  46.      aEmail.Send
  47.      'If rst.EOF Then Exit Do
  48.     rst.MoveNext
  49.  
  50. Loop
  51.     End If
  52.  
  53. rst.Close
  54. Set rst = Nothing
  55. dbs.Close
  56. Set dbs = Nothing
  57. 'Run update to update the sent mail check box
  58.     DoCmd.SetWarnings False
  59.     DoCmd.RunSQL "UPDATE PRF SET PRF.Notified = -1 WHERE (((PRF.Notified)=0))"
  60.     DoCmd.SetWarnings True
  61.     MsgBox "All new emails have been sent for posted PRF", vbInformation, "Thank You"
  62. End Sub
Oct 25 '16 #1
6 1154
jforbes
1,107 Recognized Expert Top Contributor
I would try something like the following instead:
Expand|Select|Wrap|Line Numbers
  1. Do While Not rst.EOF
  2.     If rst![EmployeeEmail] = strEmailAddress Then
  3.         aEmail.Display
  4.         signature = aEmail.Body
  5.         aEmail.Subject = "Posted payment Request"
  6.         aEmail.Body = "Invoice Number: " & rst![Invoice Number] & "" & " - " & "Vendor Name: " & rst![Vendor Name] & vbNewLine & signature
  7.     End If
  8.     rst.MoveNext
  9. Loop
This way, there is only one way out of the Loop, after everything has been accomplished.
Oct 26 '16 #2
abenny
4 New Member
That reads all the records but puts each invoice in an individual email instead of one per Employee with multiple invoice lines
Oct 26 '16 #3
jforbes
1,107 Recognized Expert Top Contributor
What does your code look like now?
Oct 26 '16 #4
abenny
4 New Member
Expand|Select|Wrap|Line Numbers
  1. Set dbs = CurrentDb
  2. Set rst = dbs.OpenRecordset("Email")
  3.  'Count of unsent e-mails
  4. intCount = DCount("[ID]", "[PRF]" _
  5. , "[Notified]=0")
  6. 'If count of unsent e-mails is zero then the procedure will not run
  7. 'If count of unsent e-mails is greater than zero, msgbox will prompt
  8. 'to send mail.
  9.      If intCount = 0 Then
  10.         MsgBox ("You have " & intCount & " emails to send.") _
  11.         , vbInformation, "Posted PRF"
  12.         Exit Sub
  13.     Else
  14.  
  15. rst.MoveFirst
  16. Do While rst.EOF = False
  17.  
  18.     Set aOutlook = CreateObject("Outlook.Application")
  19.     Set aEmail = aOutlook.CreateItem(0)
  20.     strEmailAddress = rst![EmployeeEmail]
  21.     aEmail.To = strEmailAddress
  22.         If rst![EmployeeEmail] = strEmailAddress Then
  23.          aEmail.Display
  24.          signature = aEmail.Body
  25.          aEmail.Subject = "Posted payment Request"
  26.          aEmail.Body = "Invoice Number: " & rst![Invoice Number] & "" & " - " & "Vendor Name: " & rst![Vendor Name] & _
  27.          vbNewLine & signature
  28.          End If
  29.          If rst.EOF Then Exit Do
  30.         'Loop
  31.      aEmail.Send
  32.     rst.MoveNext
  33. Loop
  34.     End If
  35. rst.Close
  36. Set rst = Nothing
  37. dbs.Close
  38. Set dbs = Nothing
Oct 26 '16 #5
abenny
4 New Member
Expand|Select|Wrap|Line Numbers
  1. Set dbs = CurrentDb
  2. Set rst = dbs.OpenRecordset("Email")
  3.  'Count of unsent e-mails
  4. intCount = DCount("[ID]", "[PRF]" _
  5. , "[Notified]=0")
  6. 'If count of unsent e-mails is zero then the procedure will not run
  7. 'If count of unsent e-mails is greater than zero, msgbox will prompt
  8. 'to send mail.
  9.      If intCount = 0 Then
  10.         MsgBox ("You have " & intCount & " emails to send.") _
  11.         , vbInformation, "Posted PRF"
  12.         Exit Sub
  13.     Else
  14.  
  15. rst.MoveFirst
  16. Do While rst.EOF = False
  17.  
  18.     Set aOutlook = CreateObject("Outlook.Application")
  19.     Set aEmail = aOutlook.CreateItem(0)
  20.     strEmailAddress = rst![EmployeeEmail]
  21.     aEmail.To = strEmailAddress
  22.         If rst![EmployeeEmail] = strEmailAddress Then
  23.          aEmail.Display
  24.          signature = aEmail.Body
  25.          aEmail.Subject = "Posted payment Request"
  26.          aEmail.Body = "Invoice Number: " & rst![Invoice Number] & "" & " - " & "Vendor Name: " & rst![Vendor Name] & _
  27.          vbNewLine & signature
  28.          End If
  29.          If rst.EOF Then Exit Do
  30.         'Loop
  31.      aEmail.Send
  32.     rst.MoveNext
  33. Loop
  34.     End If
  35. rst.Close
  36. Set rst = Nothing
  37. dbs.Close
  38. Set dbs = Nothing
Oct 26 '16 #6
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.
Expand|Select|Wrap|Line Numbers
  1. Public Sub SendMail1()
  2.  
  3.     Dim dbs As DAO.Database
  4.     Dim rst As DAO.Recordset
  5.     Dim strSubject As String
  6.     Dim strEmailAddress As String
  7.     Dim strEMailMsg As String
  8.     Dim intCount As Integer
  9.     Dim aOutlook As Object
  10.     Dim aEmail As Object
  11.     Dim sSQL As String
  12.     Dim bFirstTime As Boolean
  13.  
  14.     'Count of unsent e-mails
  15.     intCount = DCount("[ID]", "[PRF]", "[Notified]=0")
  16.  
  17.     If intCount = 0 Then
  18.         'If count of unsent e-mails is zero then the procedure will not run
  19.         MsgBox ("You have " & intCount & " emails to send."), vbInformation, "Posted PRF"
  20.         Exit Sub
  21.     Else
  22.         'If count of unsent e-mails is greater than zero, msgbox will prompt to send mail.
  23.         If vbYes = MsgBox("You have " & intCount & " emails to send, continue?", vbQuestion, "Posted PRF") Then
  24.  
  25.             ' Setup email loop
  26.             bFirstTime = True ' Don't attempt to send an email before it's ready.
  27.             Set aOutlook = CreateObject("Outlook.Application") ' Only need to do this once
  28.             sSQL = "SELECT * FROM Email ORDER BY EmployeeEmail, [Vendor Name]"
  29.             Set dbs = CurrentDb
  30.             Set rst = dbs.OpenRecordset(sSQL)
  31.  
  32.             ' Loop through Invoices
  33.             If Not rst.EOF Then
  34.                 Do While Not rst.EOF
  35.                     If strEmailAddress <> rst![EmployeeEmail] Then
  36.  
  37.                         ' EmployeeEmail has changed,
  38.                         ' So send off the current Email (if there is one)
  39.                         ' and create a New one.                        
  40.                         If Not bFirstTime Then
  41.                             ' Send off previously created Email
  42.                             aEmail.Body = strEMailMsg
  43.                             aEmail.Send
  44.                         Else
  45.                             bFirstTime = False
  46.                         End If
  47.  
  48.                         '  Create new Email
  49.                         strEmailAddress = rst![EmployeeEmail]
  50.                         Set aEmail = aOutlook.CreateItem(0)
  51.                         aEmail.To = strEmailAddress
  52.                         aEmail.Subject = "Posted payment Request"
  53.                         aEmail.Display
  54.                     End If
  55.  
  56.                     ' Add Invoice to current Email
  57.                     strEMailMsg = strEMailMsg & "Invoice Number: " & rst![Invoice Number] & "" & " - " & "Vendor Name: " & rst![Vendor Name] & vbNewLine
  58.                     rst.MoveNext
  59.                 Loop
  60.                 If Not bFirstTime Then
  61.                     ' Send Last email, since this hasn't been done yet.
  62.                     aEmail.Body = strEMailMsg
  63.                     aEmail.Send
  64.                 End If
  65.             End If
  66.  
  67.             ' Cleanup.  Don't worry about the DB Reference
  68.             rst.Close
  69.             Set rst = Nothing
  70.  
  71.             'Run update to update the sent mail check box
  72.             sSQL = "UPDATE PRF SET PRF.Notified = -1 WHERE (((PRF.Notified)=0))"
  73.             dbs.Execute sSQL
  74.             MsgBox "All new emails have been sent for posted PRF", vbInformation, "Thank You"
  75.  
  76.         End If
  77.     End If
  78. End Sub
Oct 26 '16 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
1793
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...
1
4007
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...
1
2003
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
3
2583
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...
4
13246
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!
2
5805
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.... ...
4
4319
anfetienne
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. ...
1
2556
LeighW
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...
7
4641
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...
4
3481
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...
0
7319
Oralloy
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,...
0
7376
jinu1996
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...
0
7485
tracyyun
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...
0
5623
agi2029
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,...
1
5042
isladogs
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...
0
3191
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1542
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 ...
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.