Results 1 to 9 of 9
  1. #1
    ijo68 is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Feb 2021
    Posts
    39

    SQL INSERT INTO Appends duplicate Records

    I'm Using a simple SQL Statement to insert a record into a backup table.


    Code:
    DoCmd.RunSQL "INSERT INTO tblContactsBU SELECT * FROM tblContacts " & _
    "WHERE ID=" & ID

    The Problem is it inserts duplicate records into tblContactsBU... Any idea what may cause this.

    Thanks

  2. #2
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,421
    Not clear what you mean.
    Could be that duplicate records are appended at the same time - even if the table was empty it ends up with duplicates.
    Could be that the query appends records that are identical to records that are already there.
    In the latter case, easiest might be to set one or more indexes (no dupes) on table field(s) and the query will fail to append those records. When that's working you could deal with any warnings you get.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    ijo68 is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Feb 2021
    Posts
    39
    This is in the 'before-update' of the form properties so a copy is sent to the tblContactBU as a log of the record before changes are made. Only it sends two of the same record.

  4. #4
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,421
    Then I guess we'd have to see the code for that event since the append is done via vba. However, that may not be enough as there could be other events at play. Could it be that the code stores an 'original' record plus one that is thought to be an edited record but it turns out it's not? Best approach would be for you to put a break point at the beginning of the event and step through it, looking for the cause for the 2 appends and see what you discover.

    As mentioned, you can prevent table dupes by setting indexes on one or more fields.
    Last edited by Micron; 12-15-2021 at 07:53 PM. Reason: added comment
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    ijo68 is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Feb 2021
    Posts
    39
    the only problem with the index is that the record may be edited more than once over time.. I'll put a break as mentioned..Thanks

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    If you want to provide db for analysis, follow instructions at bottom of my post.

    Do you have a date/time field in BU table to document when record was created?
    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.

  7. #7
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,421
    I'm not understanding. Edits are one thing; appends are another. If indexed with no dupes allowed, it doesn't matter - you can't duplicate the indexed fields either way. If it is a composite index, you cannot duplicate the combination. The only exception for composite index would be if the index Ignore Nulls setting is Yes. In that case you could have duplicate values in the other field or combination of fields. If you don't have a composite index you can ignore that for now.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    ijo68 is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Feb 2021
    Posts
    39
    Found the problem.. I used a calculated field in the query to grab the name of the user from the login form , Click image for larger version. 

Name:	query.png 
Views:	12 
Size:	2.6 KB 
ID:	46888 the textbox on the form with this field as it control source had a different name..(txtchgUser)

    Thanks for the help

  9. #9
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,421
    Glad to see that you have a solution.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. VBA only appends on second run?
    By schulzy175 in forum Programming
    Replies: 20
    Last Post: 04-09-2018, 03:39 PM
  2. Replies: 2
    Last Post: 11-08-2017, 09:04 AM
  3. Replies: 1
    Last Post: 01-24-2017, 08:26 AM
  4. Replies: 5
    Last Post: 12-15-2015, 07:11 AM
  5. Replies: 2
    Last Post: 08-12-2012, 10:56 AM

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