Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?
e.g
Product Code
124318N Mkate
124318N Mkate
141652G Rice
Sub AddToList()
With dbData.Recordset
For i = 1 To lvprod.ListItems.Count
 .AddNew
    .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)
    .Update
    Next i
dbData.Refresh
End With
    End Sub
Posted

1 solution

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.
 
Share this answer
 
Comments
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900