|
周末一直未上线,看到还未解决,,,
其实很简单,稍微修改下就行了,
- 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:X" & Sheets("数据库").Range("b" & Sheets("数据库").Rows.Count).End(xlUp).Row _
- & "] A LEFT JOIN (Select * From 数据库) D ON A.单号=D.单号 WHERE D.单号 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
复制代码
|
|