Sub POInsertIntoSQLServerAndUpdateOldPO()
On Error GoTo ErrHandle
If MsgBox("请注意不可以更新之前的记录,只能更新最新的订单记录,当天的记录可以多次更新都没有关系", vbYesNo + vbInformation, "提示") = vbNo Then
Exit Sub
End If
Dim i As Long
Dim LastRow As Long
Dim Arr()
Dim POSht As Worksheet
Set POSht = Sheet5
With POSht
If .FilterMode Then
.ShowAllData
End If
LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
If LastRow < 2 Then
MsgBox "没有需要更新数据,请确认是否已经导入PO清单", vbInformation
Exit Sub
End If
Arr = .Range(.Cells(1, 1), .Cells(LastRow, 23))
End With
Dim Cnn As Object
Set Cnn = CreateObject("adodb.connection")
Dim Rs As Object
Set Rs = CreateObject("adodb.recordset")
With Cnn
.Provider = SQLConnectionStr
.Open
End With
Dim Sql As String
Cnn.begintrans
For i = 2 To UBound(Arr)
Sql = "select 采购订单 from PORecord where 采购订单=N'" & Trim(Arr(i, 1)) & "' and 序号=N'" & Trim(Arr(i, 8)) & "'"
Rs.Open Sql, Cnn, 1, 3
If Rs.EOF Then
Sql = "insert into PORecord (采购订单,供应商代码,供应商名称,工厂,工厂名称,订货时间,创建时间,序号,零件属性,零件号,零件名称,数量,更新后数量,实收数量,更新后实收数量,单位,交货日期,交货地点,项目号,预计到货时间1,预计到货数量1,预计到货时间2,预计到货数量2,备注,供应商备注,最后更新人员,最后更新日期)" _
& "values (N'" & Trim(Arr(i, 1)) & "',N'" & Trim(Arr(i, 2)) & "',N'" & Trim(Arr(i, 3)) & "',N'" & Trim(Arr(i, 4)) & "',N'" & Trim(Arr(i, 5)) & "',N'" & Trim(Arr(i, 6)) & "',N'" & Trim(Arr(i, 7)) & "',N'" & Trim(Arr(i, 8)) & "',N'" & Trim(Arr(i, 9)) & "',N'" & Trim(Arr(i, 10)) & "',N'" & Trim(Arr(i, 11)) & "',N'" & Trim(Arr(i, 12)) & "','0',N'" & Trim(Arr(i, 13)) & "','0',N'" & Trim(Arr(i, 14)) & "',N'" & Trim(Arr(i, 15)) & "',N'" & Trim(Arr(i, 16)) & "',N'" & Trim(Arr(i, 17)) & "',N'" & Trim(Arr(i, 18)) & "',N'" & Trim(Arr(i, 19)) & "',N'" & Trim(Arr(i, 20)) & "',N'" & Trim(Arr(i, 21)) & "',N'" & Trim(Arr(i, 22)) & "',N'" & Trim(Arr(i, 23)) & "',N'" & UCase(Application.UserName) & "',N'" & Format(Now, "yyyy/mm/dd hh:mm:ss") & "') "
Else
Sql = "update PORecord set 更新后数量=N'" & Trim(Arr(i, 12)) & "' , 更新后实收数量=N'" & Trim(Arr(i, 13)) & "' , 最后更新人员=N'" & UCase(Application.UserName) & "' , 最后更新日期=N'" & Format(Now, "yyyy/mm/dd hh:mm:ss") & "' where 采购订单=N'" & Trim(Arr(i, 1)) & "' and 序号=N'" & Trim(Arr(i, 8)) & "'"
End If
Rs.Close
Cnn.Execute (Sql)
Next
Cnn.Committrans
Set Rs = Nothing
Cnn.Close
Set Cnn = Nothing
MsgBox "订单已经更新完成!", vbInformation
Exit Sub
ErrHandle:
MsgBox "订单更新过程出错,没有任何数据被更新,可能的原因是数据格式不准确", vbInformation
End Sub
|