[RESOLVED] Checking the contents of a combobox?
Can someone tell me why this code works exactley the opposite of the way I thought it should?
Code:
If cbKeyWords.FindStringExact(tbAddEdit.Text) = True Then
' Save entry to database
else
MsgBox("Keyword Already Exists")
End If
I have a combobox (cbKeyWords). I want to make sure tbAddEdit.Text does not already exist before I save. To me I should be testing for a False result not True. But the above code, goes to the msgbox if findstringexact=false and saves the data if it's true. Am I crazy or am I missing something? Shouldn't FindStringExact return false if it's not found? thanks
Re: Checking the contents of a combobox?
The FindExactString function returns an Integer, not a Boolean. It returns the Index of the first occurrence of a specific String within the Combobox.Items collection. More info about the function on MSDN: ComboBox.FindStringExact Method (String)
If you are looking for a Boolean result for validation, use the Contains function of Combobox.Items collection. [on MSDN: ComboBox.ObjectCollection.Contains Method]
In your case, your code could be changed to:
Code:
If cbKeyWords.Items.Contains(tbAddEdit.Text) = True Then
' Save entry to database
Else
MsgBox("Keyword Already Exists")
End If
Re: Checking the contents of a combobox?
FindStringExact doesn't return a boolean. it returns an integer indicating the index it found the string at, which if it didn't find a match would be -1, which when converted to a boolean is true.
Re: Checking the contents of a combobox?
Thanks! I actually tried to use .Contains first but I was getting an error. I was using cbKeyWords.Contains() instead of cbKeyWords.Items.Contains(). Thanks for your help.