stop duplicate entry.

zulqzulq Member Posts: 204
Hi,
I have a field in table which has to be unique, nonetheless it cannot be primary key as well. I was just wondering if there's any way to stop double entry in such situation. Any ideas please!!!
Few years ago we were not existing and few years to come we would be in the grave! So what will benefit us in the grave?

Answers

  • ritesh.singhritesh.singh Member Posts: 123
    Coding.... :-k

    RecVendor.SETRANGE(RecVendor."T.I.N. No.","T.I.N. No.");
    IF RecVendor.COUNT >1 THEN
    ERROR('Tin No. already exists')
    ELSE
    RecVendor.RESET;

    if the field gets entry from the form write the code on the onafterValidate of the field as well.
    Thanks,
    Ritesh K Singh
  • zulqzulq Member Posts: 204
    Hi singh, below is my code and I put it on OnValidate and OnInsert but it doesn't work:
    val.SETRANGE(val.No, No);
    IF val.COUNT > 1 THEN BEGIN
    ERROR('Number already exist!');
    END;
    
    I also tried the code below before posting, but it also doesn't work:
    IF val.get(No) THEN BEGIN
    ERROR('Number already exist!');
    END;
    
    Few years ago we were not existing and few years to come we would be in the grave! So what will benefit us in the grave?
  • DenSterDenSter Member Posts: 8,304
    IF RecVendor.COUNT >1 THEN
    That's not a very efficient way to do this. Better:
    IF NOT RecVendor.ISEMPTY THEN
    
  • kapamaroukapamarou Member Posts: 1,152
    Code: Select all
    IF val.get(No) THEN BEGIN
    ERROR('Number already exist!');
    END;

    Get works ONLY on the primary key.
  • ritesh.singhritesh.singh Member Posts: 123
    zulq wrote:
    Hi singh, below is my code and I put it on OnValidate and OnInsert but it doesn't work:
    val.SETRANGE(val.No, No);
    IF val.COUNT > 1 THEN BEGIN
    ERROR('Number already exist!');
    END;
    

    Forgot to write if "val.findfirst then;";
    try this:

    val.SETRANGE(val.No, No);
    if Val.findfirst then;
    IF not val.isempty THEN
    ERROR('Number already exist!');
    Thanks,
    Ritesh K Singh
  • kapamaroukapamarou Member Posts: 1,152
    if Val.findfirst then;

    You don't need this. ISEMPTY is enough and skip the database call to select the first record.
  • zulqzulq Member Posts: 204
    Thanks to all of you...
    It's working...
    Few years ago we were not existing and few years to come we would be in the grave! So what will benefit us in the grave?
  • ritesh.singhritesh.singh Member Posts: 123
    Ooops...
    mixed two codes...
    :oops:
    Thanks,
    Ritesh K Singh
Sign In or Register to comment.