|
- Sub updateaddRecords() '更新数据库已经存在记录,插入不存在记录
- Dim cnn As New ADODB.Connection
- Dim rst As New ADODB.Recordset
- Dim myPath As String
- Dim mytable As String
- Dim strTemp As String
- Dim SQL As String
- Dim strMsg As String
- Dim arrFields As Variant
- myPath = ThisWorkbook.Path & "\员工管理.accdb"
- mytable = "员工档案"
- cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" & myPath '连接数据库
- SQL = " SELECT B.* FROM [Excel 12.0;Database=" & ThisWorkbook.FullName & ";].[Sheet1 (7)$A2:K] B " _
- & "LEFT JOIN " & mytable & " A ON A.员工编号=B.员工编号 WHERE A.员工编号 IS NULL"
- '''//A这数据库表,B为工作薄的表
- Set rst = New ADODB.Recordset
- rst.Open SQL, cnn, adOpenKeyset, adLockOptimistic
- If rst.RecordCount > 0 Then '如果工作表中含有数据库不存在记录
- SQL = "INSERT INTO " & mytable & SQL ''///'插入数据库不存在记录
- cnn.Execute SQL
- End If
- rst.Close
- cnn.Close
- Set rst = Nothing
- Set cnn = Nothing
- Exit Sub
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|