VB Code:
If MsgBox("Are you shure you want to clear the list?", vbYesNo, "Clear List?") = vbYes Then For x = 0 To cboMovies.ListCount cboMovies.RemoveItem (x) Next Else Exit Sub End If
Printable View
VB Code:
If MsgBox("Are you shure you want to clear the list?", vbYesNo, "Clear List?") = vbYes Then For x = 0 To cboMovies.ListCount cboMovies.RemoveItem (x) Next Else Exit Sub End If
For x = 0 To cboMovies.ListCount - 1
BTW, you don't need to go through the items one by one, you can just do
cboMovies.Clear
Plus, you should probably loop through backwards to avoid an Index Out of Bounds error.
:)
If MsgBox("Are you shure you want to clear the list?", vbYesNo, "Clear List?") = vbYes Then
For x = cbomovies.ListCount - 1 To 0 Step -1
cbomovies.RemoveItem (x)
Next
Else
Exit Sub
End If
Also, no parentheses around the "x"VB Code:
cbomovies.RemoveItem x
Here's what's going on......
The first time through, x= 0, so it removes the first item in the list. Everything shifts down one position.VB Code:
If MsgBox("Are you shure you want to clear the list?", vbYesNo, "Clear List?") = vbYes Then For x = 0 To cboMovies.ListCount cboMovies.RemoveItem (x) Next Else Exit Sub End If
Second time through, x= 1, it deletes the second item, which use to be the third item, it's fine, until about halfway through, when X > number of items left.
Instead try, always removing just the first item. Like this.
VB Code:
If MsgBox("Are you shure you want to clear the list?", vbYesNo, "Clear List?") = vbYes Then For x = 0 To cboMovies.ListCount cboMovies.RemoveItem 0 Next Else Exit Sub End If
If speed is importaint, use Martin's solution: .Clear
And you sure want to make sure that sure is spelled correctly ;)