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
Code:
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
That helps, sort of ...
In your OP, your unexpected output was ...
124318N Mkate
124318N Mkate
141652G Rice
Where did Rice come from?
Did it appear even though the ListBox contained only 1 item?
EDIT:
Two other thoughts:
Is it possible that you got 2 instances of Mkate because you ran
your app twice (perhaps while you were testing it) and your app did
not "recognize" that a record for Mkate already existed?
If that isn't the issue (ie, your RecordSet truly has no records before
each time you run your app), have you tried setting a BreakPoint and
stepping through the loop. Could the 2nd instance of Mkate be coming
from somewhere else?
Thanx for the rep Spoo.. when I just put one product. Mkate in the list box. the reicpt i generate from that sale has only that 1 product. but when i put 2 products mkate and rice the receipt shows
124318N Mkate
124318N Mkate
141652G Rice
just as the db table looks.. I dont know whats wrong. i was thinkin it was the loop adding products to the db table. the for loop.
Spoo .. The Table I store the Records is Temporary Until the transcation is printed. then the table is cleared. so that the next transaction can take place. the table holds only one transaction per go. wen the receipt is printed the table is cleared out.So theres no chance of the records recurring.
All I have at this point is my suggestion #2 in post #4 -- step through the code.
BTW, in your "problem" situation, how many records are in the temp recordset?
If there are only 2 (as there should be), then perhaps the problem stems from
the sub (which you did not show) that does the printing itself.
If ts just one record then theres no repetation. But if there are 2 or more records then the repation occurs. the sub printing gets the value from the temp table. Heres the whole code for the Pos System.
I'm guessing that Private Function thermalprint() does the
printing.
I did not read all your code, but if the above guess is correct,
perhaps you could put a breakpoint in this function to see
how many times it is being called and if it is working as you intended.