Results 1 to 4 of 4

Thread: [RESOLVED] Checking the contents of a combobox?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    307

    Resolved [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
    Last edited by dkahn; Aug 7th, 2009 at 06:23 PM.

  2. #2
    Addicted Member garyjohn_2000's Avatar
    Join Date
    Apr 2005
    Location
    Timbaland
    Posts
    243

    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
    Last edited by garyjohn_2000; Aug 7th, 2009 at 06:49 PM.


    Anyone who has never made a mistake has never tried anything new. - Einstein
    Peace!

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    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.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    307

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width