将两列从 Excel 导入现有的 Access2010 table 使用 transferspreadsheet 时出错

Import two columns from Excel into existing Access2010 table gives error using transferspreadsheet

我有一个excel电子表格,有12列,我只需要根据最后两列导入数据(其余的需要填写最后一列。)

Excel table

Acces table

如图所示,我在 Access 中有一个名为 "ProjektDelledning" 的 table,其中两列 DelledningID 和 SaneringsmetKode 已经存在。如果 DelledningID 已经存在,则需要更新 SaneringsmetKode,如果 table 中不存在,则需要添加该值。所以 Delledning 258 应该在导入后在 Acces 中有一个 SaneringsmetKode = 1 等等。

到目前为止我试过使用这个:

Public Function Import2Columns()
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "ProjektDelledning", "C:\Users\JGJ\Desktop\Sanering.xlsx", True, "Concatenate!L:M" 
End Function

我收到一条错误消息,指出 Access 无法将所有数据附加到 table。 0 条记录中的字段内容已被删除,并且 0 条记录因密钥违规而丢失...

值得注意的是,在Accesstable中,ProjektID和DelledningID都是key。

关于如何使用 transferspreadsheet 或其他方法正确导入的任何帮助都有效。我想使用宏进行导入。

一种方法可能是导入临时 table(或只是 link 范围)。

然后,使用此 运行 组合 update/append 查询。

来自 Smart Access 的这个旧提示是我的最爱之一:

Update and Append Records with One Query

By Alan Biggs

Did you know that you can use an update query in Access to both update and add records at the same time? This is useful if you have two versions of a table, tblOld and tblNew, and you want to integrate the changes from tblNew into tblOld.

Follow these steps:

  1. Create an update query and add the two tables. Join the two tables by dragging the key field of tblNew onto the matching field of tblOld.

  2. Double-click on the relationship and choose the join option that includes all records from tblNew and only those that match from tblOld.

  3. Select all the fields from tblOld and drag them onto the QBE grid.

  4. For each field, in the Update To cell type in tblNew.FieldName, where FieldName matches the field name of tblOld.

  5. Select Query Properties from the View menu and change Unique Records to False. (This switches off the DISTINCTROW option in the SQL view. If you leave this on you'll get only one blank record in your results, but you want one blank record for each new record to be added to tblOld.)

  6. Run the query and you'll see the changes to tblNew are now in tblOld.

    This will only add records to tblOld that have been added to tblNew. Records in tblOld that aren't present in tblNew will still remain in tblOld.