|
再来一个先删除全部旧数据再导入全部新数据
Sub 删除再导入ACCESS数据库()
' 引用Microsoft ActiveX Data Objects 2.x Library
Dim cnn As New ADODB.Connection, rs As New ADODB.Recordset, SQL As String, strMsg As String
Dim DatabaseFuPass As String: DatabaseFuPass = ThisWorkbook.Path & "\模具库.accdb"
Const myPass As String = "1234"
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Jet OLEDB:Database Password=" & myPass & ";Data Source=" & DatabaseFuPass
' 清除旧数据
SQL = "delete * from 数据库"
cnn.Execute SQL
' -----------
SQL = "SELECT A.* FROM [Excel 12.0;Database=" & ThisWorkbook.FullName & ";].[数据库$A1:Z" & Sheets("数据库").Range("b" & Sheets("数据库").Rows.Count).End(xlUp).Row _
& "] A LEFT JOIN (Select * From 数据库) B ON A.单号=B.单号 WHERE B.单号 IS NULL"
rs.Open SQL, cnn, adOpenKeyset, adLockOptimistic
' 导入数据
SQL = "INSERT INTO 数据库 " & SQL
cnn.Execute SQL
strMsg = rs.RecordCount & "条记录已添加到数据库!"
MsgBox strMsg, vbInformation, "提示"
rs.Close: cnn.Close
Set rs = Nothing: Set cnn = Nothing
End Sub
|
|