Results 1 to 8 of 8

Thread: Add to listbox only if word is not already there

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    North East America
    Posts
    463

    Post

    Ok what am i doing wrong? I have a listbox which I add words to but the problem is if the word is in the list I want a message box to say its in the list. That part works the problem is if it is not in the list a get a run time error 380 invalid property value. I know it is in my for next loop but and the counter part but can't seem to get it right!

    Here is my code maybe some one can figure out what is wrong Thanks Troy

    Private Sub CmdAdd_Click()
    Dim FileNumber
    Dim NewWord As String
    Dim CheckWord As Integer
    FileNumber = FreeFile
    'Open App.Path & "\ana.txt" For Append As #FileNumber
    NewWord = TxtWord.Text
    For I = 1 To LstWord.ListCount
    I = I + 1
    LstWord.ListIndex = I
    Counter = Counter + 1
    inlist = LstWord.Text
    If inlist = NewWord Then
    MsgBox "This word " & NewWord & " is already in the list"
    TxtWord.Text = ""
    TxtWord.SetFocus
    Exit Sub
    Else
    If Counter = LstWord.ListCount Then
    Open App.Path & "\ana.txt" For Append As #FileNumber
    Print #FileNumber, NewWord
    LstWord.AddItem NewWord
    TxtWord = ""
    TxtWord.SetFocus
    Close #FileNumber
    End If
    End If

    Next

    End Sub

    ------------------
    Troy MacPherson
    Customer Suport Software Analyst
    [email protected]



  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Post

    Remove the I = I + 1. The For Next loop automatically increments I. What is happening currently is that for the For Next loop and your I = I + 1 are incrementing I, so you are skipping values. When you get to the bottom of the list and you then add 1 to I, your index is higher than the number of entries so you get the error.

    ------------------
    Marty

  3. #3
    Junior Member
    Join Date
    Jun 1999
    Location
    Papillion, NE, USA
    Posts
    21

    Post

    Also

    only go to ListCount - 1

    I.E.

    For I = 1 To LstWord.ListCount - 1

    ListCount is always one more than the largest ListIndex value

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Post

    I also just noticed two things:
    1) You have not Defined "I", so unless it's defined someplace else, VB is going to define it for you as a variant and variants are slow. Suggestion: If you are not already doing it, go to Tools>Options>Editor and check Require Variable Declaration. Not only will that make sure you Dim everything, but it will also catch spelling mistakes which are otherwise tough to find.
    2) Use Next I rather than Next. It's also faster.

    ------------------
    Marty

    [This message has been edited by MartinLiss (edited 11-16-1999).]

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    North East America
    Posts
    463

    Post

    Thanks all!!!! The -1 was the final trick! I had been up and down removing and adding things but when I did the -1 it all worked out. Thanks again! One big problem I had is that it would add the word to the list but then the message box woud come up and say it was already there. But the -1 cured that.

    and Marty thanks I usually have the Require Variable Declaration checked but I must have removed it for some reason.

    ------------------
    Troy MacPherson
    Customer Suport Software Analyst
    [email protected]



  6. #6
    Junior Member
    Join Date
    Nov 1999
    Posts
    21

    Post

    Place acommand button add on ur form and also place a list box.
    Try executing this code .IF it works then the same thing applies even when u open ur file and try to populate it into list box.


    Private Sub ADD_Click()
    A = InputBox("ADD", Add)
    For I = 0 To List1.ListCount
    If List1.List(I) = A Then
    MsgBox "ALREADY IN THE LIST"
    Exit For
    Else
    List1.AddItem A
    Exit For
    End If
    Next
    End Sub

    [This message has been edited by shan1 (edited 11-16-1999).]

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Post

    Did you also remove the I = I + 1? If you don't, then, for example, the third word in a 3-word list will not be found. Try it.

    ------------------
    Marty

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    North East America
    Posts
    463

    Post

    Yes I did get rid of the I=I+1 and that did help. Thanks!!!

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