Results 1 to 2 of 2

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
    t_macpherson@yahoo.com



  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    Try this instead, it's alot simpler and quicker..
    Code:
    Private Sub CmdAdd_Click()
        Dim iFileNumber As Integer
        Dim I As Integer
        Dim sNewWord As String
        
        iFileNumber = FreeFile
        'Open App.Path & "\ana.txt" For Append As #iFileNumber
        sNewWord = LCase(txtWord.Text)
        For I = 0 To lstWord.ListCount - 1
            If sNewWord = LCase(lstWord.List(I)) Then Exit For
        Next
        If I < lstWord.ListCount Then
            'Word Found
            MsgBox "The word " & Chr(34) & sNewWord & Chr(34) & " is already in the list."
            txtWord.Text = ""
            txtWord.SetFocus
        Else
            'Word Not Found
            Open App.Path & "\ana.txt" For Append As #iFileNumber
            Print #iFileNumber, sNewWord
            lstWord.AddItem sNewWord
            txtWord = ""
            txtWord.SetFocus
            Close #iFileNumber
        End If
    
    End Sub

    ------------------
    Aaron Young
    Analyst Programmer
    aarony@redwingsoftware.com
    adyoung@win.bright.net


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

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