Results 1 to 7 of 7
  1. #1
    Benj is offline Advanced Beginner
    Windows 7 32bit Access 2016
    Join Date
    May 2018
    Posts
    33

    detecting duplicates of primary key at once


    Hi everyone! Just wanting to know if there is a after update code to immediately detect a primary key duplicate? I know it would eventually would but it happens after i've entered other fields for that duplicate. Thanks in advance!

  2. #2
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Use something like
    Code:
    If DCount("*", "TableName", "FieldName = 'SomeValue'")>0 Then
    After update is too late. Use the BeforeUpdate event so it can be cancelled if a duplicate exists
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Why would a primary key duplicate? Is this an autonumber field or a custom key constructed with code?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    Benj is offline Advanced Beginner
    Windows 7 32bit Access 2016
    Join Date
    May 2018
    Posts
    33
    Hi June7! i have to enter my own primary key. its not an autonumber. its actually a tag for an animal. sometimes instead of entering 907Y i would enter 907W which has a duplicate.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    As Colin said, do data validation in BeforeUpdate event.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    Really don't recommend you use this as a primary key - what if you type it in wrong and have to correct it? - all related tables will need to be updated as well.

    Have an autonumber primary key field and link to other tables on that. And for your tag number specify the field as indexed no duplicates. That will prevent a duplicate being added, but I would still uses the form beforeupdate event to check if the tag key already exists (or perhaps better the after update event of the tag control) if only for more control on the messaging when a duplicate is entered

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    I agree with others --use autonumber as Primary Key for the table.
    If you want to use a field for your info --eg: TagNo that you identify as indexed No Duplicates.
    I would include an error handler and check specifically for error 3022 (duplicate value).
    The DBMS will highlight this error when you attempt to add a duplicate PK, and/or add a duplicate to an index identified as No Duplicates.

    Good luck.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. detecting duplicates in child form
    By Benj in forum Access
    Replies: 1
    Last Post: 07-04-2018, 03:04 PM
  2. Detecting a Before Update Cancel
    By EddieN1 in forum Programming
    Replies: 3
    Last Post: 06-09-2013, 08:04 PM
  3. Replies: 2
    Last Post: 04-05-2012, 03:11 PM
  4. Replies: 3
    Last Post: 05-11-2011, 02:32 PM
  5. detecting a null date
    By tedpottel in forum Queries
    Replies: 3
    Last Post: 03-02-2010, 01:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums