Select CASH_B Scan cr1 = 0 acc_code = "" Scatter Memvar Select VOUCHERS Locate For Date = m.date And vou_no = m.balance; And vou_type = 'JV' SCAN WHILE Date = m.date And vou_no = m.balance; And vou_type = 'JV' If m.dr_amount>0 AND cr_amount > cr1 cr1 = cr_amount ac1 = acccode ENDIF ENDSCAN Select CASH_B Replace ACC_CODE With ac1 endscanIt is not tested code. Also, performance also depend on whether you have proper index for search expression.