Click here to Skip to main content
14,327,936 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi Experts. I dont know wats wrong with the loop below. But when i call this function its supposed to insert the records in a listbox into a db access db table. but it saves the first record twice. Wat could i be doing wrong?
Product Code
124318N Mkate
124318N Mkate
141652G Rice
Sub AddToList()
With dbData.Recordset
For i = 1 To lvprod.ListItems.Count
    .Fields(0) = lvprod.ListItems(i).Text 'prod code
    .Fields(1) = lvprod.ListItems(i).SubItems(2)
    .Fields(2) = txtPrice.Text  'price
    .Fields(3) = lvprod.ListItems(i).SubItems(1) 'qty
    .Fields(4) = 0
    .Fields(5) = lvprod.ListItems(i).SubItems(4)
    .Fields(6) = Month(Now)
    .Fields(7) = Year(Now)
    Next i
End With
    End Sub

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

I'm not sure this helps, but why are you using For i = 1 To lvprod.ListItems.Count? Aren't Lists zero based in VB? If so, I'd change it to For i = 0 To lvprod.ListItems.Count - 1, then step through the code in the debugger to see where it's getting something wrong.
Patoh58 19-Jun-11 14:20pm
Thanx walt.. But that produced an error.. I used the For i = 1 To lvprod.ListItems.Count to point to the first record in the listbox to the number of records the listbox has.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100