我有一个Access表。这些字段与Excel工作簿的字段匹配。 Access表包含Excel工作表没有的一些记录,反之亦然。但是,有一些重叠。示例:
访问表1
ID Name Attribute
1 Canada A
2 USA B
3 Mexico A
Excel
Name Attribute
USA B
Mexico A
France B
Excel中存在但Access中不存在的数据应添加到Access表中。预期输出:
ID Name Attribute
1 Canada A
2 USA B
3 Mexico A
4 France B
现在,我在Excel工作表上打开一个记录集。我想执行类似于
的查询INSERT INTO Table1 (Name, Attribute)
SELECT (Name, Attribute) FROM Recordset
WHERE (Name, Attribute) NOT IN
(SELECT Name, Attribute FROM Table1)
在Access中可以这样做吗?我如何参考我的记录集?这是导致问题的代码:
Sub openWorksheet()
Dim cn As New ADODB.Connection
Dim rs1 As New ADODB.Recordset
Dim strQuery As String
Dim lastRowIndex As Long
Dim i As Integer
Dim wbPath As String
Dim db As DAO.Database
Dim rs2 As DAO.Recordset
wbPath = "C:\Users\david.grandmaison\Desktop\RISE\Rise_Template.xlsx"
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & wbPath & ";" & _
"Extended Properties=Excel 12.0;"
strQuery = "SELECT Dist_Ret_Door_ID FROM [Sheet1$]"
rs1.Open strQuery, cn
Set db = CurrentDb
strQuery = "This is what I'm trying to write"
db.Execute strQuery
我知道我可以在Excel记录集上循环并逐个检查每个记录,如果不存在则将其添加,但是我正在寻找单个插入内容,因为循环将花费很长时间。但是,如果我错了或者无法避免,请告诉我。