>I am still new to VFP programming.
>It appears that to add a record to a database via a form, you APPEND BLANK and then have the user enter the new data.
>I did not see any controls that would generate an error message if you tried to enter a duplicate key as happens with BTrieve and other databases.
>Under FoxPro 2.6 you can either go to EOF and SCATTER MEMVAR and not add the record if it is already present.
>Under VPF though, if the blank record is already created it would appear that you would have to do the following in the Validate control for the key field:
>
< code snipped >
> But this seems SO KLUDGY. Surely my understanding is incomplete.
>
>Thanks
>Bruce Strom
Apart from what others have told you (and what regards the primary key), you probably want to have some other key without duplicates. The simple solution is (for me) is to have this table open once more, under an appropriate order. My form class has something like this in its .init:
if not used(this.OtherAlias)
select (this.DataEnvironment.InitialSelectedAlias)
this.OtherAlias=alias()+"_2"
use (dbf()) again in 0 order 1 alias (this.otherAlias)
endif
Later, in code, if I have to check if some key value already exists in the table, I simply check seek(this.value, thisform.OtherAlias). I may use the third parameter if it's not the first tag I use. The gotcha with your approach is that if you're using row buffering, as soon as you move off your new record (which a Seek would surely do), it's implicitly TableUpdate()ing, and your blank record is saved, like it or not. This way you use the other alias for surfing along your table, and don't move off your record in the main alias.