PDA

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


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

MartinLiss
Nov 16th, 1999, 02:20 AM
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

jim
Nov 16th, 1999, 02:24 AM
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

MartinLiss
Nov 16th, 1999, 02:26 AM
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).]

Troy Mac
Nov 16th, 1999, 02:41 AM
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
t_macpherson@yahoo.com

shan1
Nov 16th, 1999, 02:46 AM
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).]

MartinLiss
Nov 16th, 1999, 04:00 AM
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

Troy Mac
Nov 16th, 1999, 07:09 AM
Yes I did get rid of the I=I+1 and that did help. Thanks!!!