Select Master Replace All pqty With qty Select purchase Replace All pqty With qty saleamount=0 cur_code="" Select sales End=Reccount() ** for loop to scroll on sales.dbf For ak=1 To End saleamount=0 cur_code="" Select sales Goto ak endloop=qty cur_code=Alltrim(codes) Select purchase Do While endloop>0 Select Master Locate For Alltrim(codes)=Allt(cur_code) And pqty>0 If Found() If pqty>endloop Replace sold With endloop saleamount=sold*Master.rate Replace pqty With pqty-endloop endloop=0 Exit Else endloop=endloop-pqty Replace sold With pqty saleamount=saleamount+(sold*rate) Replace pqty With 0 Endif Endif Select purchase Locate For Allt(codes)=Allt(cur_code) And pqty>0 If Found() If pqty>endloop Replace sold With endloop saleamount=saleamount+(sold*purchase.rate) Replace pqty With pqty-endloop endloop=0 Exit Else endloop=endloop-pqty Replace sold With pqty saleamount=saleamount+(sold*rate) Replace pqty With 0 Endif Else Exit Endif Enddo Select sales Replace amount With Round(saleamount,0) Endfor