DataBase - Duplicate Records

In my form, in a button_Click event, I have the Insert Query command. How do I prevent from inputing a duplicate record Can anyone help If you need more clerfication, just reply.


Answer this question

DataBase - Duplicate Records

  • Lampkin

    I just decided to use Access. So could you please post a code snippet as an example please. It would be appreciated.

  • oky

    The problem with FindByID() is that it works against a local copy of the data - there's no way to determine if the record exists in the data store or not. Someone could have added or removed the record from some other location or you may be working with a subset of data.

    In Access, you will probably have to write a look-up method that checks for existence of a record before deciding what to do: update or insert.

     



  • Ahmedabugh

    That all depends on the type of database you're using (SQL Server, Oracle, MySQL, etc.)

  • jan_alex

     

    You could do a DELETE FROM tablename WHERE (xxxxxx) before you do your INSERT to insure that an item is not already in there.  You could do it all in one fails swoop with "DELETE.......; (<=== semicolon) INSERT........"

     

     


  • Trenton1

    Well, if it's SQL Server, you could do this:
     
    IF NOT EXISTS(SELECT * FROM MyTable WHERE RowID = @RowID)
         INSERT INTO MyTable (...) VALUES (...);
    ELSE
         UPDATE MyTable SET ... WHERE RowID = @RowID;
     
    That way, if it exists, an update is executed; otherwise, a new record is added.
     
    But again, that all depends on the type of database you're using.


  • Tyler Whitney-MSFT

    let say your table have the primary key call "ID"

    then use the property "FindByID" to see if it is exist  then do an update or insert depend on that.

     


  • DataBase - Duplicate Records