For Loop VB6 Double Entry Malfunction.
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
Re: For Loop VB6 Double Entry Malfunction.
Patoh
At first glance, nothing pops out.
Could you perhaps attach an image of your ListBox
Spoo
1 Attachment(s)
Re: For Loop VB6 Double Entry Malfunction.
Re: For Loop VB6 Double Entry Malfunction.
Patoh
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?
Spoo
Re: For Loop VB6 Double Entry Malfunction.
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.
Re: For Loop VB6 Double Entry Malfunction.
Okay, small point, but that's a listview and not a listbox.
Re: For Loop VB6 Double Entry Malfunction.
Patoh
At the risk of being repetitive, it seems like you running your app twice
1. just with Mkate in ListView .. print
2. add Rice, rerun .. print
Sounds like the problem is that the 2nd time you run your app, it is adding
Mkate again -- because you told it to !!
Seems like you need to add an algo to check if Mkate exists in the recordset.
If it does, then do not add a second record.
EDIT:
To incorporate Marty's observation :)
Spoo
Re: For Loop VB6 Double Entry Malfunction.
Thanx Martin. My Bad.. The List View...
Re: For Loop VB6 Double Entry Malfunction.
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.
Re: For Loop VB6 Double Entry Malfunction.
Patoh
OK, good to hear. Scratch that idea off the list :)
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.
Spoo
1 Attachment(s)
Re: For Loop VB6 Double Entry Malfunction.
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.
Re: For Loop VB6 Double Entry Malfunction.
Patoh
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.
Spoo