Results 1 to 8 of 8

Thread: Loop thru Listbox not getting all records

  1. #1

    Thread Starter
    Member Suidae's Avatar
    Join Date
    Nov 2001
    Posts
    52

    Loop thru Listbox not getting all records

    I trying to loop through a listbox and retrieve e-mail addresses. I get the addresses just fine what I’m having a problem with is. If there are ten addresses I’m only pulling 9 addresses and if there is only one address it does not do anything.

    What am I doing wrong?

    VB Code:
    1. Dim n As Integer
    2.         Dim x As Integer
    3.        
    4. 'On Error GoTo err:
    5.  
    6.         n = 0
    7.         x = lstSelected.ListCount - 1
    8.    
    9.     Do Until n = x
    10.         lstSelected.ListIndex = n
    11.         adNM = InStr(1, lstSelected.Text, " - ")
    12.         adID = Right$(lstSelected.Text, Len(lstSelected.Text) - adNM - 1)
    13.         Addstr = adID
    14.         MsgBox adID
    15.         'Call SendBroadcast
    16.         n = n + 1
    17.    Loop
    I'm a misanthropic philanthropist!
    Frog, the only white meat...

  2. #2
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    Your condition should be "Do Until n > x", not "n = x"
    "It's cold gin time again ..."

    Check out my website here.

  3. #3
    Addicted Member
    Join Date
    Oct 2002
    Location
    Somewhere out in space
    Posts
    151
    when n reach .listcount - 1 when you get to the n = n + 1 it will exit the do block without taking the item at this index.
    changing
    VB Code:
    1. do until n=x
    for
    VB Code:
    1. do until n>x
    should fix that
    'You keep creatures in cages and release them to fight? That's sick!'

  4. #4
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    you should loop till n > x

    or don't substract 1 from the listcount and test on n = x

    change it to whatever you think is better.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  5. #5
    Addicted Member
    Join Date
    Oct 2002
    Location
    Somewhere out in space
    Posts
    151
    Guess i wasn't fast enough for that one
    'You keep creatures in cages and release them to fight? That's sick!'

  6. #6
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    you could rewrite it with a for next, reads better

    VB Code:
    1. For n = 0 To lstSelected.ListCount -1
    2.         lstSelected.ListIndex = n
    3.         adNM = InStr(1, lstSelected.Text, " - ")
    4.         adID = Right$(lstSelected.Text, Len(lstSelected.Text) - adNM - 1)
    5.         Addstr = adID
    6.         MsgBox adID
    7.         'Call SendBroadcast
    8.  
    9.    Next
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  7. #7

    Thread Starter
    Member Suidae's Avatar
    Join Date
    Nov 2001
    Posts
    52
    Thank you all..
    I'm a misanthropic philanthropist!
    Frog, the only white meat...

  8. #8
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    has everyone turned into burnt pancakes today???!
    VB Code:
    1. Sub woof()
    2. Dim lngIndex    As Long
    3. Dim strText     As String
    4. Dim lngPos      As Long
    5.     For lngIndex = 0 To lstSelected.ListCount - 1
    6.         strText = lstSelected.List(lngIndex)
    7.         lngPos = InStr(1, strText, " - ")
    8.         adID = Right$(strText, Len(strText) - lngPos - 1)
    9.         Addstr = adID
    10.         MsgBox adID
    11.         'Call SendBroadcast
    12.    Next lngIndex
    13. End Sub
    The basics of ALL programming...*SIGH*

    Woka

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