|
excel导入acess中,为避免重复录入,限制员工姓名和系统编号为查询是否唯一的条件,请教各位老师,我哪里出现语句错误了,
Sub addRecords()
'引用Microsoft ActiveX Data Objects 2.x Library
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim SQL As String
Dim strMsg As String
cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\数据库.mdb"
SQL = "SELECT A.* FROM [Excel 12.0;Database=" & ThisWorkbook.FullName & ";].[电子看板数据拆分$B2:F" & Range("b" & Rows.Count).End(xlUp).Row _
& "] A LEFT JOIN 工资数据 B ON A.员工姓名=B.员工姓名 AND A.系统编号=B.系统编号 WHERE B.员工姓名 IS NULL"
rs.Open SQL, cnn, adOpenKeyset, adLockOptimistic
If rs.RecordCount > 0 Then
SQL = "INSERT INTO 工资数据 " & SQL
cnn.Execute SQL
strMsg = rs.RecordCount & "条记录已添加到数据库!"
Else
strMsg = "没有发现可以插入的记录!"
End If
MsgBox strMsg, vbInformation, "提示"
rs.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing
End Sub
|
|