PDA

View Full Version : Unique items in ListBox



YasserKhalil
08-04-2010, 01:23 AM
Hi guys

I'm trying to add items to a ListBox through a TextBox on condition that the items in the ListBox are unique ..
In other words,If I tried to add a new item through the textbox a message appeared to tell me that "The item is listed in the listbox"

mikerickson
08-04-2010, 01:52 AM
Perhaps
Private Sub CommandButton1_Click()
Dim newItem As String
Dim listPointer As Long

newItem = TextBox1.Text

With ListBox1
For listPointer = 0 To .ListCount - 1
If LCase(newItem) = LCase(.List(listPointer)) Then
MsgBox newItem & " is already in the list."
Exit Sub
ElseIf LCase(newItem) < LCase(.List(listPointer)) Then
Exit For
End If
Next listPointer

.AddItem newItem, listPointer
TextBox1.Text = vbNullString
End With
End Sub