Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying Duplicate Records from Access Database

Status
Not open for further replies.

geonebula

Technical User
Apr 11, 2011
12
0
0
US
I have 3 databases with a set of redundant tables:
Source - with the field data entered (Primary Keys same as Master)
Master - containing all the merged data from the Sources
Master_Duplicate - to contain all duplicate records from an attempt to merge a Source into the Master

I have created 2 linked tables in the Master_Duplicate db for the subject table (one from the Master and the other from the Source).

Then I generate this SQL statement:

INSERT INTO [TableName](Field1,Field2,...FieldN) SELECT (Field1,Field2,...FieldN) FROM LinkTable_Source INNER JOIN LinkTable_Master ON LinkTable_Source.PrimaryKey = LinkTable_Master.PrimaryKey

and have the the Master_Duplicate database execute it. However, nothing is being inserted. What am I missing here?

The code is:
Code:
[COLOR=green]'Create link table of Duplicate Records[/color]
                    sTableName = varTableName
                    sDBPath = varItem
                    sLinkName = sTableName & "_Source"
                    sLinkName2 = sTableName & "_Master"
                    Set tblsrc = db.CreateTableDef(sLinkName)
                    tblsrc.Connect = "MS Access;DATABASE=" & sDBPath & ";TABLE=" & sTableName
                    tblsrc.SourceTableName = sTableName
                    Set tbldupsrc = dbMasterDuplicate.CreateTableDef(sLinkName)
                    tbldupsrc.Connect = "MS Access;DATABASE=" & sDBPath & ";TABLE=" & sTableName
                    tbldupsrc.SourceTableName = sTableName
                    Set tbldup = dbMasterDuplicate.CreateTableDef(sLinkName2)
                    tbldup.Connect = "MS Access;DATABASE=" & strMasterDbPath & ";TABLE=" & sTableName
                    tbldup.SourceTableName = sTableName
                    
                    On Error Resume Next [COLOR=green]'Assume error means table doesn't exist[/color]
                    db.TableDefs.Delete tblsrc.sLinkName [COLOR=green]' Delete old table link[/color]
                    db.TableDefs.Append tblsrc [COLOR=green]' Link the new table[/color]
                    db.TableDefs.Refresh 
                    
                    [COLOR=green]'Create table links in Master[/color] Duplicate database of sourcedb and masterdb
                    dbMasterDuplicate.TableDefs.Delete tbldupsrc.sLinkName [COLOR=green]' Delete old  source table link[/color]
                    dbMasterDuplicate.TableDefs.Append tbldupsrc [COLOR=green]' Link the new source table[/color]
                    dbMasterDuplicate.TableDefs.Delete tbldup.sLinkName2 [COLOR=green]' Delete old master table link[/color]
                    dbMasterDuplicate.TableDefs.Append tbldup [COLOR=green]' Link the new master table[/color]
                    dbMasterDuplicate.TableDefs.Refresh [COLOR=green]' Refresh the table list just in case[/color]
                    
                    [COLOR=green]'Place all duplicate records into Master Duplicate Database[/color]
                    strSQLDupIns = "INSERT INTO " & [varTableName] & "(" & strFieldList & ")" & " SELECT " & "(" & strFieldList & ")" & " FROM " & [sLinkName] & " INNER JOIN " _
                    & [sLinkName2] & " ON " & [sLinkName] & "." & [PK_field] & " = " & [sLinkName2] & "." & [PK_field]
                    dbMasterDuplicate.Execute strSQLDupIns
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top