Update Existing Access Records from CSV Import , native to MS Access or in VB.NET

后端 未结 1 518
轻奢々
轻奢々 2021-01-23 12:52

I am the application administrator for a ticketing system at my organization. We\'re adding a new client, and need to import their customer records into our system.

Howe

相关标签:
1条回答
  • 2021-01-23 13:28

    Essentially, you will have to do a series of action queries: append, update, and delete that use filters and joins. You can save queries as stored queries and call them by DoCmd.OpenQuery, or you can write them in VBA as strings to be passed into DoCmd.RunSQL sqlStatement or CurrentDb.Excecute sqlStatement

    IMPORT

    First, import the CSV using DoCmd.TransferText acImportDelim into a temporary table. Be sure to clean out previous csv data from temp table prior to import.

    DELETE FROM tempTable;
    

    UPDATE

    Then, update existing records between temp and live table but condition for existing customers in live table. In Access SQL, joins can be used in update queries but you may run into not updateable queries:

    UPDATE tempTable INNER JOIN liveTable 
    ON tempTable.CustomerID = liveTable.CustomerID
    SET liveTable.Col1 = tempTable.Col1,
        liveTable.Col2 = tempTable.Col2,
        liveTable.Col3 = tempTable.Col3, 
        ... ;
    

    Alternatively, to bypass non-updateable query:

    UPDATE liveTable
    SET liveTable.Col1 = DLookUp("Col1", "tempTable", "CustomerID=" & liveTable.CustomerID),
        liveTable.Col2 = DLookUp("Col2", "tempTable", "CustomerID=" & liveTable.CustomerID),
        liveTable.Col3 = DLookUp("Col3", "tempTable", "CustomerID=" & liveTable.CustomerID), 
        ... 
    WHERE CustomerID IN 
        (SELECT CustomerID FROM tempTable);
    

    Once done, clean out records just updated from temp table (to not conflict with append step and duplicate entries):

    DELETE FROM tempTable 
    WHERE CustomerID IN 
          (SELECT CustomerID FROM liveTable);
    

    APPEND

    Finally, append new records from temp table into live table but condition for customers NOT in live table which you can do with the LEFT JOIN ... NULL:

    INSERT INTO (Col1, Col2, Col3 ...) 
    SELECT Col1, Col2, Col3 ... 
    FROM tempTable LEFT JOIN liveTable 
    ON tempTable.CustomerID = liveTable.CustomerID
    WHERE liveTable.CustomerID Is Null;
    
    0 讨论(0)
提交回复
热议问题