I have been trying different ways to make this work and I wasn't 100% sure if this needed to go in the Excel or the Access forums.
I have a CSV that I have a excel macro created to do some formatting and then export the data into Access. that part works. I also have it checking for duplicate entries and skipping them. The problem that i run into is that not all my duplicates are truly duplicate, there have been updates and now i need to update Access. I have 2 fields, my primary key which is what I'm primarily matching on and then I'm checking if its been updated by checking another field "Last Changed" If this field doesn't match then I need to update the record.
At this point I'm trying to use a SQL UPDATE and I now believe that i have it formatted correct but when it gets to a record that needs to be updated I get an error: Run-time error '-2147217887 (80040e21)': The changes you requested to the table were not successful because they would create duplicate values in the index, primary key or relationship. I'm not sure what I'm doing wrong. the only other thing i could think to do would be to check each field to see if it has changed and just run the query against that but I feel like i would run into the same issue.
This is how I'm connecting to Access if that helps
And this is the Query I'm using:
The SQL code sits in a loop with an IF so it shouldn't execute every time and I plan on once i get this working to revisit this code and try to optimize it but that is not a priority currently.
I'm using both Excel 2007 and Access 2007 on XP
Any help would be much appreciated.
I have a CSV that I have a excel macro created to do some formatting and then export the data into Access. that part works. I also have it checking for duplicate entries and skipping them. The problem that i run into is that not all my duplicates are truly duplicate, there have been updates and now i need to update Access. I have 2 fields, my primary key which is what I'm primarily matching on and then I'm checking if its been updated by checking another field "Last Changed" If this field doesn't match then I need to update the record.
At this point I'm trying to use a SQL UPDATE and I now believe that i have it formatted correct but when it gets to a record that needs to be updated I get an error: Run-time error '-2147217887 (80040e21)': The changes you requested to the table were not successful because they would create duplicate values in the index, primary key or relationship. I'm not sure what I'm doing wrong. the only other thing i could think to do would be to check each field to see if it has changed and just run the query against that but I feel like i would run into the same issue.
This is how I'm connecting to Access if that helps
Code:
strTableName = "TableName"
strDBLocation = "\\xxxx\userhome\xxxx\xxxx\xxxxDatabase\Database Backend.accdb"
strConnect = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strDBLocation & ";"
Set cn = New ADODB.Connection
cn.Open strConnect
Set rs = New ADODB.Recordset
rs.Open strTableName, cn, adOpenKeyset, adLockOptimistic, adCmdTable
And this is the Query I'm using:
Code:
strSQL = "UPDATE " & strTableName & " "
strSQL = strSQL & "SET [Work Order ID] = '" & Range("B" & r).Value & "', "
strSQL = strSQL & "[JOBSTATUS] = '" & Range("C" & r).Value & "', "
strSQL = strSQL & "[SUBSTATUS] = '" & Range("D" & r).Value & "', "
strSQL = strSQL & "[Job Status] = '" & Range("E" & r).Value & "', "
strSQL = strSQL & "[STATUSDATETIME] = #" & Range("F" & r).Value & "#, "
strSQL = strSQL & "[Schedule Date] = #" & Range("G" & r).Value & "#, "
strSQL = strSQL & "[TZI] = '" & Range("H" & r).Value & "', "
strSQL = strSQL & "[Slot Start] = " & Range("I" & r).Value & ", "
strSQL = strSQL & "[Slot End] = " & Range("J" & r).Value & ", "
strSQL = strSQL & "[PRIORITY] = '" & Range("K" & r).Value & "', "
strSQL = strSQL & "[SMS Tech ID] = '" & Range("L" & r).Value & "', "
strSQL = strSQL & "[FSRUSERUID] = " & Range("M" & r).Value & ", "
strSQL = strSQL & "[Job ID] = '" & Range("N" & r).Value & "', "
strSQL = strSQL & "[DISPATCHERID] = '" & Range("O" & r).Value & "', "
strSQL = strSQL & "[KEY1] = '" & Range("P" & r).Value & "', "
strSQL = strSQL & "[Order Type] = '" & Range("Q" & r).Value & "', "
strSQL = strSQL & "[KEY2] = '" & Range("R" & r).Value & "', "
strSQL = strSQL & "[WO Reason Code] = '" & Range("S" & r).Value & "', "
strSQL = strSQL & "[Order Date/Time] = #" & Range("T" & r).Value & "#, "
strSQL = strSQL & "[ESTSTDATETIME] = #" & Range("U" & r).Value & "#, "
strSQL = strSQL & "[TECHCOMPLETED] = '" & Range("V" & r).Value & "', "
strSQL = strSQL & "[ROUTINGAREACODE] = '" & Range("W" & r).Value & "', "
strSQL = strSQL & "[Routing Area] = '" & Range("X" & r).Value & "', "
strSQL = strSQL & "[Last Changed] = #" & Range("Y" & r).Value & "#, "
strSQL = strSQL & "[Account ID] = '" & Range("Z" & r).Value & "', "
strSQL = strSQL & "[First Name] = '" & Range("AA" & r).Value & "', "
strSQL = strSQL & "[Last Name] = '" & Range("AB" & r).Value & "', "
strSQL = strSQL & "[Phone 1] = '" & Range("AC" & r).Value & "', "
strSQL = strSQL & "[Phone 2] = '" & Range("AD" & r).Value & "', "
strSQL = strSQL & "[CUSTOMERID1] = '" & Range("AE" & r).Value & "', "
strSQL = strSQL & "[CUSTOMERID2] = '" & Range("AF" & r).Value & "', "
strSQL = strSQL & "[Street Address] = '" & Range("AG" & r).Value & "', "
strSQL = strSQL & "[City] = '" & Range("AH" & r).Value & "', "
strSQL = strSQL & "[STATE] = '" & Range("AI" & r).Value & "', "
strSQL = strSQL & "[ZIP Code] = " & Range("AJ" & r).Value & ", "
strSQL = strSQL & "[TIMESLOTDESC] = '" & Range("AK" & r).Value & "', "
strSQL = strSQL & "[ORDERINGOPERATORID] = '" & Range("AL" & r).Value & "', "
strSQL = strSQL & "[CONTACTNAME] = '" & Range("AM" & r).Value & "', "
strSQL = strSQL & "[CONTACTPHONE1] = " & Range("AN" & r).Value & ", "
strSQL = strSQL & "[CONTACTPHONE2] = '" & Range("AO" & r).Value & "', "
strSQL = strSQL & "[SMS Job Number] = " & Range("AP" & r).Value & ", "
strSQL = strSQL & "[SPA] = " & Range("AQ" & r).Value & " "
strSQL = strSQL & "WHERE [WORKORDERUID] = " & Range("A" & r).Value & ";"
cn.Execute strSQL
The SQL code sits in a loop with an IF so it shouldn't execute every time and I plan on once i get this working to revisit this code and try to optimize it but that is not a priority currently.
I'm using both Excel 2007 and Access 2007 on XP
Any help would be much appreciated.