Results 1 to 12 of 12

Thread: ListBox ListIndex = -1

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    11

    Question

    I've got a listbox (lstLeft) that stores 10 possible values. I've got a recordset that returns those that match for the corresponding agent. So I might have one that matches and I might have more.

    I'm trying to write code that will loop through the ItemData of the items in lstLeft, if it matches, move the item from lstLeft to lstRight, so that all matching values for the selected agent will appear when I edit his/her file.

    Problem is, my ListIndex value keeps coming up with a value of -1. My code is shown below...any thoughts? Is there a better way to tackle what I'm trying to accomplish?


    Do While Not objRS.EOF
    If lstLeft.ItemData(i) = objRS("ID") Then
    lstRight.AddItem lstLeft.Text
    lstRight.ItemData(lstRight.NewIndex) = lstLeft.ItemData(i)
    lstLeft.RemoveItem lstLeft.ListIndex
    Else
    i = i + 1
    End If
    Loop
    TM

  2. #2
    Addicted Member
    Join Date
    Sep 2000
    Posts
    219
    ListIndex = -1 when nothing is selected from the list.
    Perhaps you might consider changing your code like this:


    For i = 0 to lstLeft.ListCount - 1
    objRS.FindFirst "[Fld]='" & lstLeft.List(i) & "'"
    If objRS.NoMatch = True Then
    lstRight.AddItem lstLeft.List(i)
    lstRight.ItemData(lstRight.NewIndex) = lstLeft.ItemData(i)
    lstLeft.RemoveItem i
    End If
    Next i


    I have assumed that objRS has a field called Fld

    Let me know if this works.

  3. #3
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    617

    Hi!

    I would not do it this way....

    But...
    Just one point
    Where r u looping through the recordset with MOVENEXT?

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    11
    Thanks for the info. For starters, I don't have a objRS.NoMatch, so I get an error:

    "Method or data member not found"

    Also, don't have a FindFirst either.
    TM

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    11
    Lafor - thanks for the reply.

    I inadvertently left out the MoveNext when I posted my message. I have it as shown below... I'm interested in hearing how you would approach my situation. Thanks!

    Do While Not objRS.EOF
    If lstLeft.ItemData(i) = objRS("ID") Then
    lstRight.AddItem lstLeft.Text
    lstRight.ItemData(lstRight.NewIndex) = lstLeft.ItemData(i)
    lstLeft.RemoveItem lstLeft.ListIndex
    Else
    i = i + 1
    End If
    objRS.MoveNext
    Loop
    TM

  6. #6
    Addicted Member
    Join Date
    Sep 2000
    Posts
    219
    Try this then.

    For i = 0 to lstLeft.ListCount - 1
    objRS.MoveFirst
    objRS.Find "[Fld]='" & lstLeft.List(i) & "'"
    If objRS.Eof = True Then
    lstRight.AddItem lstLeft.List(i)
    lstRight.ItemData(lstRight.NewIndex) = lstLeft.ItemData(i)
    lstLeft.RemoveItem i
    End If
    Next i



  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    11
    Error: Rowset cannot scroll backwards on line:

    objRS.Find "ID='" & lstLeft.List(i) & "'"
    TM

  8. #8
    Addicted Member
    Join Date
    Sep 2000
    Posts
    219
    Does ID have an integer value? If so replace the line

    objRS.Find "ID='" & lstLeft.List(i) & "'"

    with the line

    objRS.Find "ID=" & lstLeft.List(i)

  9. #9

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    11
    Problem is that << lstLeft.List(i) >> is getting the text value of the listbox entry when I need the ItemData value of that entry because that's what will match the ID from the RecordSet.
    TM

  10. #10
    Addicted Member
    Join Date
    Sep 2000
    Posts
    219
    If that is the problem, this should work.

    For i = 0 to lstLeft.ListCount - 1
    objRS.MoveFirst
    objRS.Find "[ID]='" & lstLeft.ItemData(i) & "'"
    If objRS.Eof = True Then
    lstRight.AddItem lstLeft.List(i)
    lstRight.ItemData(lstRight.NewIndex) = lstLeft.ItemData(i)
    lstLeft.RemoveItem i
    End If
    Next i

    If it does, can you give me info about the error (just to improve my knowledge):
    "Rowset cannot scroll backwards"

    I have never come across that error before.

  11. #11

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    11

    Red face

    Shafee - I get the same error. I'm very close to resolving this with my original code too, by the way.

    Not sure what to tell you about the error because it's the first time I've encountered it as well.

    Thanks for your help. If I get it resolved, I'll post my code for all to see.
    TM

  12. #12

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    11

    Thumbs up code that works...

    i = 0
    Do While Not objRS.EOF
    Do While True
    If i >= lstLeft.ListCount Then
    Exit Do
    End If
    If lstLeft.ItemData(i) = objRS("ID") Then
    lstRight.AddItem lstLeft.List(i)
    lstRight.ItemData(lstRight.NewIndex) = lstLeft.ItemData(i)
    lstLeft.RemoveItem i
    Exit Do
    Else
    i = i + 1
    End If
    Loop
    i = 0
    objRS.MoveNext
    Loop
    TM

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