具有相同列名的访问表。
我正在尝试将excel数据表插入到访问表中,但是想要应用WHERE子句,以便它忽略访问表中已有的日期。
我很乐意将所有数据插入到访问表中,但无法成功应用WHERE子句忽略现有日期。令人讨厌的是,我无法正确引用实际的表名,因此我引用了表单(但工作正常)。
这是我到目前为止的代码:
Sub TestInsert()
Dim cnn As ADODB.Connection
Dim dbPath As String
Dim xlPath As String
Dim uSQL As String
dbPath = "C:\Test\TestDB.accdb"
xlPath = "C:\Test\TestXL.xlsm"
'Set Connection string
Set cnn = New Connection
cnnstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath
'Open connection to db
cnn.Open cnnstr
uSQL = "INSERT INTO tbl_ApolloBrochurewareReportTEST " _
& "SELECT * FROM [Excel 12.0;HDR=YES;DATABASE=" & xlPath & "].[Sheet1$]"
cnn.Execute uSQL
cnn.Close
End Sub
任何输入或知识都会很棒,谢谢。
答案 0 :(得分:1)
您应该能够扩展SQL表达式:
uSQL = "INSERT INTO tbl_ApolloBrochurewareReportTEST " _
& "SELECT * FROM [Excel 12.0;HDR=YES;DATABASE=" & xlPath & "].[Sheet1$] As T " & _
"WHERE T.Date NOT IN (SELECT [Date] FROM tbl_ApolloBrochurewareReportTEST)"