I have checked lastkey() in the Help but cannot find any clue.
I decided to add a flag, so the code becomes
thisflag=vinvdetail.flagselect stock_id from vinvdetail intocurs temp where stock_id=this.value and thisflag<>vinvdetail.flagif_tally<>0waitwindows'This item already exist...'timeout1endif