×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Insert into with values but ignoring duplicates

Insert into with values but ignoring duplicates

Insert into with values but ignoring duplicates

(OP)
I've got a procedure that is first splitting up a field and creating new records; then immediately dumping those records into an assigned table.
Currently, if I enter the same record number over and over, duplicate records are just added into the table instead of overwriting changes to the fields.

Here's an example of what's happening:

Source Table
Parent | Child
-------------
1234 | 13, 27, 189, 210

This is the code that does the formatting:

CODE

Set d = CurrentDb
    Set R = d.OpenRecordset("SELECT* From OrdTbl WHERE [OrdId] = " & [Forms]![frmSampleChildOrd]![txtSampleNumber])
    With R
        If Not .BOF And Not .EOF Then
            While Not .EOF

                vParent = .Fields("OrdId") & ""
                vChild = .Fields("ChildOrd")
                ARR = Split(vChild, ",") 

which makes the data look like this:
Parent | Child
-------------
1234 | 13
1234 | 27
1234 | 189
1234 | 210

Which then gets dumped into Destination Table.

CODE

For i = LBound(ARR) To UBound(ARR)
                    sSQL = "INSERT INTO tblSampleChildOrd (ParentOrd,ChildOrd) VALUES ('" & vParent & "','" & ARR(i) & "')"
                    'Debug.Print sSQL
                    CurrentDb.Execute sSQL, dbFailOnError
                Next

                Erase ARR

                .MoveNext
            Wend
        End If 

So, if I enter 1234 again, I get this:
Parent | Child
-------------
1234 | 13
1234 | 27
1234 | 189
1234 | 210
1234 | 13
1234 | 27
1234 | 189
1234 | 210

I do not want duplicate Child values. How can I adjust my Insert statement to make that happen?

RE: Insert into with values but ignoring duplicates

You can do:

CODE

Set R = d.OpenRecordset("SELECT DISTINCT * From OrdTbl WHERE ... 

Or, how about checking if this value is already in the table before Insert

CODE

For i = LBound(ARR) To UBound(ARR)
    If IsNull(DLookup(yada, yada, yada)) Then
        sSQL = "INSERT INTO tblSampleChildOrd (ParentOrd,ChildOrd) VALUES ('" & vParent & "','" & ARR(i) & "')"
        'Debug.Print sSQL
        CurrentDb.Execute sSQL, dbFailOnError
    End If
Next 


---- Andy

There is a great need for a sarcasm font.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close