Hello there,

I'm creating a loop to read the records in the recordset, and compare it to the items in a listbox where it determines if the user is trying to add a duplicate data.

Here are the codes.

VB Code:
  1. Private Sub List2_DblClick(Cancel As Integer)
  2. Rem Insert Answers Into Question
  3. Dim a As String
  4.  
  5. rst.Open "select * from ss_table", CurrentProject.AccessConnection, adOpenDynamic, adLockOptimistic
  6. rst2.Open "select * from ss_table", CurrentProject.AccessConnection, adOpenDynamic, adLockOptimistic
  7.  
  8. If Not rst.EOF Then
  9.  
  10.     rst2.MoveFirst
  11.  
  12. '------loops through the recordset (rst2)------
  13.      Do While Not rst2.EOF
  14.         a = rst2!ansID
  15.         rst2.MoveNext
  16.         Loop
  17. rst2.Close
  18.  
  19. rst1.Open "SELECT sID from autonumber", CurrentProject.AccessConnection, adOpenDynamic, adLockOptimistic
  20.    
  21. '-----prevent adding duplicate items------
  22. If List2.Column(0, List2.ListIndex) = a Then
  23.         MsgBox "Invalid"
  24.         Else
  25.        
  26.         rst.AddNew
  27.         rst!sID = "ss" & Right("000000" & CStr(CInt(rst1!sID) + 1), 6)
  28.         rst!qnsID = Me.List1.Column(0, List1.ListIndex)
  29.         rst!ansID = Me.List2.Column(0, List2.ListIndex)
  30.         Rem List Box Insert
  31.         Me.List3.AddItem List2.Column(0, List3.ListIndex) & ";" & List2.Column(1, List3.ListIndex)
  32.        
  33.         End If
  34.  
  35.     rst.Update
  36.     rst1.Close
  37.     rst1.Open "update autonumber set sID = sID +1", CurrentProject.AccessConnection, adOpenDynamic, adLockOptimistic
  38.     rst.Close
  39. End If
  40. End Sub

The problem im experiencing now is that, whenever i add a new item twice, it will prompt 'invalid' on the second attempt,

but if i add a different item and then return to add the original item (which already exists in the recordset)

it will be added despite being a duplicate.

I believe the mistake is within my Do While Not rst2.EOF loop.

Need some help.

Thank you very much

Astro