|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
先谢谢大家,我想把工作表“PoList"中的数据更新到D盘的DataBase.mdb的表”PoList"中,条件是当“客户品番”、"客户订单号码"、“单价"都一致。现在出现更新不了,新追加的就可以的情况,请帮忙看看代码错在哪里?
Sub PoInput()
On Error Resume Next
Dim cnn As Object
Dim rs As Object
Dim myPath As String
Dim myTable As String
Dim s As String
Dim sql As String
Dim arr As Variant
Dim i As Integer
Set cnn = CreateObject("adodb.Connection")
Set rs = CreateObject("adodb.Recordset")
myTable = "PoList"
cnn.ConnectionString = "provider=Microsoft.jet.OLEDB.4.0;Persist security info= False; Data source= D:\Database.mdb"
cnn.Open
p = Sheets("PoList").[A65536].End(3).Row
arr = Sheets("PoList").Range("A1:M" & p)
For i = 1 To UBound(arr, 2)
s = s & ",a." & arr(1, i) & "=b." & arr(1, i)
Next
sql = "update " & myTable & " a,[Excel 8.0;Database=" & ActiveWorkbook.FullName & "].[PoList$" _
& Range("a1").CurrentRegion.Address(0, 0) & "] b set " & Mid(s, 2) & " where a.客户品番=b.客户品番 and a.客户订单号码=b.客户订单号码 and a.单价=b.单价"
cnn.Execute sql
sql = "select a.* from [Excel 8.0;Database=" & ActiveWorkbook.FullName & "].[PoList$" & Range("a1").CurrentRegion.Address(0, 0) _
& "] a left join " & myTable & " b on a.客户品番=b.客户品番 and a.客户订单号码=b.客户订单号码 and a.单价=b.单价 where b.客户订单号码 is null"
rs.Open sql, cnn, 1, 3
If rs.RecordCount > 0 Then
sql = "insert into " & myTable & " " & sql
cnn.Execute sql
MsgBox rs.RecordCount & "行数据已经添加到数据库!", vbInformation, "添加数据"
Else
MsgBox "工作表的数据数据库中已经存在,均已更新。", vbInformation, "更新数据"
End If
rs.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing
End Sub
|
|