-
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
-
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.
-
Hi!
I would not do it this way....
But...
Just one point
Where r u looping through the recordset with MOVENEXT?
-
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.
-
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
-
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
-
Error: Rowset cannot scroll backwards on line:
objRS.Find "ID='" & lstLeft.List(i) & "'"
-
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)
-
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.
-
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.
-
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.
-
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