Click to See Complete Forum and Search --> : no duplicates


Archie
April 14th, 2001, 08:59 AM
Using the code below, what code would I have to add in order to ensure that an item is added only one time? (if I already added it to the second list and tried to add it again using the command button, an error msg should pop up) Thank you.

private Sub cmdAdd_Click()
Dim i as Integer
for i = 0 to List1.ListCount - 1
If List1.Selected(i) = true then List2.AddItem List1.List(i)
End If
next
End Sub

private Sub Form_Load()
Dim aLine as string
Open App.Path & "\grocery.txt" for input as #1 Do While Not EOF(1)
Line input #1, aLine
List1.AddItem aLine
Loop
Close #1
End Sub




Archie Kantzavelos

shree
April 14th, 2001, 11:07 AM
The following code should work satisfactorily for a short grocery list.


private Sub cmdAdd_Click()
Dim i as Integer, j as integer
dim Found as boolean
for i = 0 to List1.ListCount - 1
If List1.Selected(i) = true then
found = false
for j = 0 to List2.ListCount -1
found = (list2.list(j)=list1.list(i))
if found then exit for
next
if not found then
List2.AddItem List1.List(i)
else
msgbox "error"
endif
End If
next
End Sub