I did something likewise a while back and, with Yisrael´s help, I created something that creates unique coupons/vouchers by checking if it already exists in db, and if so, tiries another code. Look here:
Note: this generates random numbers, so not incremental.