Results 1 to 8 of 8

Thread: What's wrong with this loop?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    What's wrong with this loop?

    VB Code:
    1. If MsgBox("Are you shure you want to clear the list?", vbYesNo, "Clear List?") = vbYes Then
    2.     For x = 0 To cboMovies.ListCount
    3.         cboMovies.RemoveItem (x)
    4.     Next
    5. Else
    6. Exit Sub
    7. End If

  2. #2

  3. #3

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Plus, you should probably loop through backwards to avoid an Index Out of Bounds error.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    India
    Posts
    342
    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
    ksm

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Also, no parentheses around the "x"
    VB Code:
    1. cbomovies.RemoveItem x

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Here's what's going on......
    VB Code:
    1. If MsgBox("Are you shure you want to clear the list?", vbYesNo, "Clear List?") = vbYes Then
    2.     For x = 0 To cboMovies.ListCount
    3.         cboMovies.RemoveItem (x)
    4.     Next
    5. Else
    6. Exit Sub
    7. End If
    The first time through, x= 0, so it removes the first item in the list. Everything shifts down one position.
    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:
    1. If MsgBox("Are you shure you want to clear the list?", vbYesNo, "Clear List?") = vbYes Then
    2.     For x = 0 To cboMovies.ListCount
    3.         cboMovies.RemoveItem 0
    4.     Next
    5. Else
    6. Exit Sub
    7. End If

    If speed is importaint, use Martin's solution: .Clear
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

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