从 CSV 导入、MS Access 或 VB.NET 原生更新现有访问记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32440734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update Existing Access Records from CSV Import , native to MS Access or in VB.NET
提问by schizoid04
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.
我是我组织中一个票务系统的应用程序管理员。我们正在添加一个新客户,需要将他们的客户记录导入我们的系统。
However, we have been denied access to simply grab these records from a direct database connection.
但是,我们无法从直接数据库连接中简单地获取这些记录。
We are limited to entering their ticketing system, and running an export of the existing records to a CSV file.
我们只能进入他们的票务系统,并将现有记录导出为 CSV 文件。
In order to make this work with our system, I'm taking these CSV's and entering them into an MS Access database, which our system will read and import/update records on our ticketing system from.
为了在我们的系统中进行这项工作,我将这些 CSV 文件输入到 MS Access 数据库中,我们的系统将从该数据库读取并导入/更新我们的票务系统上的记录。
However, I cannot find a way to updaterecords in the MS Access db from records in the CSV.
但是,我找不到从 CSV 中的记录更新MS Access 数据库中的记录的方法。
I can import records into a table, but it skips any records that already exist (There is a field included in the customer data that is being used as a unique identifier / primary key to distinguish new / existing records).
我可以将记录导入到表中,但它会跳过任何已存在的记录(客户数据中包含一个字段,该字段被用作唯一标识符/主键以区分新/现有记录)。
Any records which already contain this primary key are simply skipped, it does not update the records in the ms access db containing that key.
任何已经包含此主键的记录都会被简单地跳过,它不会更新包含该键的 ms access 数据库中的记录。
I do not have much experience with MS Access other than creating basic tables and forms; it's outside my normal scope of work.
除了创建基本的表格和表单之外,我对 MS Access 没有太多经验;这超出了我的正常工作范围。
I need to find a way to take a CSV file containing records that may or may not already be in this ms access db, and create new records if not contained within the CSV, or update records if they are contained in the CSV.
我需要找到一种方法来获取 CSV 文件,该文件包含可能已经或可能不在此 ms access 数据库中的记录,如果未包含在 CSV 中,则创建新记录,或者如果它们包含在 CSV 中,则更新记录。
It doesn't necessarily matter how this is done, I could implement this using vb.net or a macro, if you could provide a way to do this with either.
如何做到这一点并不重要,我可以使用 vb.net 或宏来实现这一点,如果你能提供一种方法来做到这一点的话。
I understand that this is a bit off-guidelines, I tried to find ways to do this on my own and haven't been able to come up with any code to test out and post as a starting point (my apologies).
我知道这有点偏离准则,我试图找到自己的方法来做到这一点,但一直无法想出任何代码来测试并作为起点发布(我的歉意)。
回答by Parfait
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 sqlStatementor CurrentDb.Excecute sqlStatement
本质上,您必须执行一系列操作查询:使用过滤器和连接的追加、更新和删除。您可以将查询保存为存储查询并通过 调用它们DoCmd.OpenQuery,或者您可以在 VBA 中将它们编写为要传递到DoCmd.RunSQL sqlStatement或CurrentDb.Excecute sqlStatement
IMPORT
进口
First, import the CSV using DoCmd.TransferText acImportDeliminto a temporary table. Be sure to clean out previous csv data from temp table prior to import.
首先,将 CSV 导入DoCmd.TransferText acImportDelim到临时表中。请务必在导入之前清除临时表中以前的 csv 数据。
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 updateablequeries:
然后,更新临时表和实时表之间的现有记录,但更新实时表中现有客户的条件。在 Access SQL 中,连接可用于更新查询,但您可能会遇到不可更新的查询:
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:
最后,将临时表中的新记录附加到实时表中,但客户的条件不在实时表中,您可以使用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;

