First, I'd like to point out that letting users enter a field that is your primary key is a very bad practice. A primary key should be a key that you generate automatically in your system, and the user doesn't usually even see it, let alone change it.
Now, here is a routine I use to check for duplicates in a field:
lparameters pNewVal, pFld, pCurRec
local lFnd, lOldRec, lOldAli
lFnd = .F.
lOldRec = recno()
lOldAli = alias()
select 0
use dbf(lOldAli) again alias DupChk
locate for eval(pFld) = pNewVal
if found()
do while .T.
if recno() = pCurRec
continue
else
lFnd = .T.
exit
endif
if eof()
exit
endif
enddo
endif
if lFnd
messagebox('Cannot enter duplicates in this field',48,'Duplicate found')
endif
use in DupChk
select (lOldAli)
return not lFnd
>HI All...........
>I Have table Customer with field id, name
>
>Field Id --> Primary Key
>
>*****
>Textbox1, controlsource=id
>Textbox2, controlsource=name
>
># When I Click in command button Add :
> Select 0
> append blank
>
># When I Save data with The same id An Error Occurs *** unique of index violation ***
> Select 0
> TableUpdate(.T.)
>
># I Want to Ask
>1. How to Check if a user typing with the same id, How to display existing data??? or How to Handle it
>2. How to manage Error Handler *** unique index violated ***
>
>Regards