How would I prevent a Duplicate ListBox entry?
So if Apple is entered the user can not repeat and enter Apple again
It seems I should loop through this but how exactly?
vbMarkO
Printable View
How would I prevent a Duplicate ListBox entry?
So if Apple is entered the user can not repeat and enter Apple again
It seems I should loop through this but how exactly?
vbMarkO
The loop way:
VB Code:
For i = 0 To List1.ListCount - 1 If List1.List(i) = "Apple" Then MsgBox "Apple already entered" End If Next i
You can also use SendMessage, though I forget the actual message (somebody'll most likely post it).
ThanxQuote:
Originally posted by jemidiah
The loop way:
VB Code:
For i = 0 To List1.ListCount - 1 If List1.List(i) = "Apple" Then MsgBox "Apple already entered" End If Next i
You can also use SendMessage, though I forget the actual message (somebody'll most likely post it).
LB_FINDSTRINGEXACT or LB_FINDSTRINGQuote:
Originally posted by jemidiah
You can also use SendMessage, though I forget the actual message (somebody'll most likely post it).
Here is a complete example that you can modify for either a listbox or a combobox. Using SendMessage will be much faster that looping through the listbox or combobox.
VB Code:
Private Declare Function SendMessageString Lib "user32" Alias "SendMessageA" _ (ByVal hWnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ ByVal lParam As String) As Long Private Const CB_FINDSTRINGEXACT = &H158 Private Const LB_FINDSTRINGEXACT = &H1A2 Dim lngRetVal As Long lngRetVal = SendMessageString(MyListBox.hWnd, _ LB_FINDSTRINGEXACT, -1&, _ "string to find") If lngRetVal > -1& Then ' It's already in the list and lngRetVal is the listindex Else MyListBox.Additem "string to find" End If