Chiang,
Change your cod to this;
** This code is in the click event of the NEW button.
** the invsetup lookup table is also in the data environment of the form.
*******
LOCAL lcAlias
lcAlias = ALIAS()
SELECT InvSetup
meminv=0
meminvnext=0
&& Get next Invoice/do number
DO WHILE NOT RLOCK()
ENDDO
meminv=invsetup.nextinv
replace nextinv with nextInv + 1
UNLOCK
IF NOT EMPTY(lcAlias)
SELECT ( lcAlias )
ENDIF
RETURN meminv
******
Two things have been added, 1) the selected alias is proitected and 2) the invoice number table is locked before the next inv number is played with. The first insure that the calling environment is restored on teh way out of the routine and the second insures that there will never be duplication invoice numbers generated by this code.