PDA

Click to See Complete Forum and Search --> : Add to listbox only if word is not already there


Troy Mac
Nov 16th, 1999, 02:07 AM
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

Aaron Young
Nov 16th, 1999, 02:54 AM
Try this instead, it's alot simpler and quicker..

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).]