avoid duplicating records

lucianaluciana Member Posts: 43
Hi to all,
how can I avoid duplicating records in a table?
If I insert a new record, I shold check the other records and look at the values were written beforee : if I have same values I should have an error message

Thanks
Luciana

Comments

  • mohana_cse06mohana_cse06 Member Posts: 5,504
    IF you want to do it on No. 2 field of Item table then write
    No. 2 - OnValidate()
    IF "No. 2" <> '' THEN BEGIN
      Item_M.RESET;
      Item_M.SETRANGE("No. 2","No. 2");
      IF Item_M.FINDFIRST THEN
        IF Item_M."No." <> "No." THEN
          ERROR('Altery Entered.');
    END;
    
  • rhpntrhpnt Member Posts: 688
    Setting a primary key with the right field(s) always does the trick...
  • lucianaluciana Member Posts: 43
    IF you want to do it on No. 2 field of Item table then write
    No. 2 - OnValidate()
    IF "No. 2" <> '' THEN BEGIN
      Item_M.RESET;
      Item_M.SETRANGE("No. 2","No. 2");
      IF Item_M.FINDFIRST THEN
        IF Item_M."No." <> "No." THEN
          ERROR('Altery Entered.');
    END;
    

    I have this code on my table:
    Object Number - OnValidate()
    ObjectTable.RESET;
    ObjectTable.SETRANGE(ObjectTable.Nr, Rec.ObjectType); 
       IF ObjectTable.FIND('-') THEN
          Rec.ObjectType := ObjectTable.Nr;
    
    
    That I need to filter Number by Object Type..
    Where I should put the code you suggested?
  • mohana_cse06mohana_cse06 Member Posts: 5,504
    I have just given an example..based on that you have to create your logic..

    where do you want to have this logic? in which table and which field? what are the primary keys of that table?
  • SogSog Member Posts: 1,023
    after "IF ObjectTable.FIND('-') THEN"
    begin
    lvarRec.setrange(objecttype,objecttable.Nr);
    lvarrec.setrange("object number","Object number");
    lvarrec.setfilter("primary key",'<>%1',"primary key");//if you modify the record and this piece of code is called, you won't get the error)
    if not lvarrec.isempty then
    error('already did this');
    rec.objecttype := objecttable.nr;

    But I suggest to have a decent primary key, so you won't have to write this code. Because:
    No PK: if you insert a record in this table without calling the validation, it will be inserted without error.
    PK: same but with error.
    |Pressing F1 is so much faster than opening your browser|
    |To-Increase|
  • lucianaluciana Member Posts: 43
    I apologize for editing this post.
    I forgave to set the primary key I needed.
    Problem solved.
    Thanks

    Luciana
Sign In or Register to comment.