tags:

views:

70

answers:

3

i have 2 tables in access

this is what i need:

1. if the PK from table1 exists in table2, then delete the entire record with that PK from table2 and add the entire record from table1 into table2
2. if the PK does not exist then add the record

i need help with both the sql statement and the VBA

i guess the VBA should be a loop, going through every record in table1. inside the loop i should have the select statement

+1  A: 

I would do this in two statements. One that deletes the proper rows and another to insert the row.

Dim oDB As DAO.Database
Dim sSQL As String
Dim oQry As DAO.QueryDef

Set oDB = DBEngine.Workspaces(0).Databases(0)
sSQL = "Delete From Table2 Where Exists( Select 1 From Table1 Where Table1.Id = Table2.Id )"
oDB.Execute sSQL, dbFailOnError

sSQL = "PARAMETERS [Col1Param] Text, [Col2Param] Text, [Col2Param] Text; " & _
    "Insert Into Table1(Col1, Col2, Col3) Values([Col1Param], [Col2Param], [Col3Param])"
Set oQry = oDB.CreateQueryDef("", sSQL)
oQry!Col1Param = "Col1Value"
oQry!Col2Param = "Col2Value"
oQry!Col3Param = "Col3Value"
oQry.Execute, dbFailOnError

oQry.Close

Set oQry = Nothing
Set oDB = Nothing
Thomas
+1  A: 

I don't think you need a VBA loop, just two SQL statements.

First delete the matching rows from table2.

DELETE 
FROM table2 AS m
WHERE pk IN (SELECT pk FROM table1);

Then append all the rows from table1 into table2.

INSERT INTO table2 (
    pk,
    field2,
    field3,
    field4)
SELECT
    i.pk,
    i.field2,
    i.field3,
    i.field4
FROM
    table1 AS i;
HansUp
You should change the `WHERE` in your `DELETE` statement to either check if table2's pk is in the list, or constrain the inner query to only return rows where the pk matches.
VeeArr
@VeeArr Thanks. Rather than revising the faulty EXISTS, I converted to IN.
HansUp
+1  A: 
DELETE FROM table2
  WHERE EXISTS
  (SELECT * FROM table1, table2
   WHERE table1.pk=table2.pk);

INSERT INTO table2
SELECT * FROM table1;

This assumes table1 and table2 have the same columns.

VeeArr
cool what don't you like about the hansup's solution?
I__
As written, it should delete all rows from table2 (instead of just the ones in table1). You need to either change the `EXISTS` to match mine, or change `Exists (SELECT pk FROM table1)` to `m.pk IN (SELECT pk FROM table1)`.
VeeArr
His solution has since been correct using the second method I suggested. It should work fine now.
VeeArr
The delete solution assumes there is no useful data in the records being deleted, which I would say is an unwarranted assumption in about 99.99% of the circumstances where this would be useful. You really have to have a firmly enforced master/slave relationship for that to work.
David-W-Fenton
This behavior is specified as part of the OP.
VeeArr
+1 for fenton cause he's the man!
I__
@VeeArr: you're right. I was confusing this question with http://stackoverflow.com/questions/2992242/if-exists-update-else-insert-new-record . I find the tendency of certain people to post slight variations on the same question to be quite annoying, and it leads to things like this (I come across the variant and don't read the question thoroughly because I think I'm just seeing the same question again).
David-W-Fenton