Results 1 to 10 of 10
  1. #1
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    237

    Check for duplicate in one part of a table


    Hi to all,
    I have a query qryPersoneels based on table tblPersonnels.
    A form frmPersonnels is based on the query as a Record Source with Filter [DateDeparted] IS NULL (Only part of the table rows are slelected). When I have a new person entered on the form I know if it is a duplicate or not. How can I check if this new person has a duplicate in the rows of the table where [DateDeparted] IS NOT NULL ?

    Khalil

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    iF isnull(DLookup(...) ) then ‘ no duplicate

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,424
    use the dcount function to look up the name. If it returns 0, there is no duplicate, otherwise it will return 1 or more depending on number of duplicates. Perhaps use the control beforeupdate event

    Code:
    if dcount("*","tblPersonnel","[DateDeparted] IS NOT NULL AND personname='" & txtPersonName & "'")>0 then
        msgbox "duplicate exists"
    else
        'carry on
    end if
    
    

  4. #4
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    237
    Hi,
    I am having error 2471 because a duplicate is defined by a card number. Each person has a unique card number
    What adjustments need to be done ?
    I tried this
    Code:
    If DCount("*", "tblPersonnel", "[DateDeparted] IS NOT NULL AND CardNumber=" & txtyCardNumber) > 0 Then
    Khalil

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,424
    names need to match what you have - would also help if you provided the text to the error as this usually tells you exactly what the problem is

    "tblPersonnel", "[DateDeparted] IS NOT NULL AND CardNumber=" & txtyCardNumber


    it may be you need to use

    not isnull([DateDeparted])

  6. #6
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    237
    Hi,
    I used the following and it worked.
    Code:
    If DCount("*", "tblPersonnel", "[CardNumber]=" & txtCardNumber & "AND [DateDeparted] IS NOT NULL") > 0 Then
    Thank you all

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,927
    If each card number is unique, how can you have a dupe?
    Plus that does not take into account someone withe the same card number that has not left?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    237
    Hi Welshgasman
    The table has all records for all persons: those departed (left) and those who did not. Originally the card number was not unique which allowed having duplicates and data was added to the table.

    The card number field property was indexed= Yes , duplicates OK. So I have to remove the duplicates first, then, change the indexed: Yes, no duplicates. When this is done, no need for the DCount function to be used because in this case when a new record is added it should be saved to the table (having all persons even if the query has only persons with DateDeparted is null) and the default access error message will be shown. Is my analyses correct?

    Khalil

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,927
    Yes, you should get an error message if No duplicates are chosen.
    However I would have thought that you would need to re-number any cardnumber duplicates, else the index could not be created?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  10. #10
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,424
    I would also add the standard access message is not very user friendly so you may want to keep the code for a more friendly message

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

Similar Threads

  1. Replies: 3
    Last Post: 04-18-2017, 02:40 AM
  2. Replies: 2
    Last Post: 07-09-2014, 04:28 AM
  3. Replies: 17
    Last Post: 11-05-2013, 04:19 AM
  4. Replies: 3
    Last Post: 09-26-2012, 01:39 PM
  5. check duplicate records within same table
    By cthorn112 in forum Queries
    Replies: 0
    Last Post: 06-19-2012, 12:23 PM

Tags for this Thread

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