title: How to write VBA with Do While Loop?
body: <p>I have a table with 3 fields: <code>DONOR_CONTACT_ID,</code> <code>RECIPIENT_CONTACT_ID</code>, <code>ORDER_NUMBER</code>. I want to sort <code>DONOR_CONTACT_ID</code> in ascending order which I did with my query <code>Q_RECIPIENT_SORT</code>. Then I want to use temporary variables to check to see if the records have the same <code>DONOR_CONTACT_ID</code> and then display a message if they do (Most of the records have the same <code>DONOR_CONTACT_ID</code>). My program does everything it is supposed to, but at the end it always gets an error that says "No Current Record". Here is my code: </p>
  
  <pre><code>Option Compare Database
  Option Explicit
  
  
  Function UsingTemps()
  
  Dim dbs As DAO.Database
  Dim rst As DAO.Recordset
  Dim strTemp1 As Long
  Dim strTemp2 As Long
  
  DoCmd.SetWarnings False
  DoCmd.OpenQuery ("Q_RECIPIENT_SORT")
  DoCmd.OpenTable ("T_RECIPIENT_SORT")
  DoCmd.SetWarnings True
  Set dbs = CurrentDb
  
  Set rst = dbs.OpenRecordset("T_RECIPIENT_SORT", dbOpenTable)
  
  rst.MoveFirst
  strTemp1 = rst!DONOR_CONTACT_ID
  rst.MoveNext
  strTemp2 = rst!DONOR_CONTACT_ID
  
  Do While Not (rst!DONOR_CONTACT_ID = rst.EOF)
  
  If strTemp1 = strTemp2 Then
  MsgBox ("Equal")
  
  Else
  MsgBox ("Not equal")
  
  End If
  
  strTemp1 = strTemp2
  rst.MoveNext
  strTemp2 = rst!DONOR_CONTACT_ID
  
  Loop
  
  Set dbs = Nothing
  
  End Function
  </code></pre>
  
  <p>I think the problem is with the following lines: </p>
  
  <pre><code>rst.MoveNext
  
  strTemp2 = rst!DONOR_CONTACT_ID
  </code></pre>
  
  <p>I think it is trying to move to the next record when there are no more records left. Probably something wrong with my logic. But I've been staring at it for a while and my changes haven't worked. I need another set of eyes to take a look at it. </p>
  
  <p>Any help is appreciated!</p>