|
楼主 |
发表于 2017-4-19 19:32
|
显示全部楼层
- Sub AddOrChangeValues2() '物流计划导入数据库
- Dim mydata As String, mytable As String, SQL As String
- Dim cnn As ADODB.Connection
- Dim rs As ADODB.Recordset
- Dim rsx As ADODB.Recordset
- Dim i As Long, n As Long, j As Long
-
- On Error Resume Next
- mydata = ThisWorkbook.Path & "\数据中心\物控中心.accdb"
- mytable = "物流计划"
- n = Worksheets("数据透视").Cells(Rows.Count, 1).End(xlUp).Row
- Set cnn = New ADODB.Connection
- With cnn
- .Provider = "Microsoft.ACE.OLEDB.12.0"
- .Open mydata
- End With
- mysql = "select * from " & mytable
- Set rs = New ADODB.Recordset
- rs.Open mysql, cnn, adOpenKeyset, adLockOptimistic
- For i = 2 To n
- SQL = "select * from " & mytable & " where 供应商='" & Cells(i, 2).Value & "'and 到货日期=#" & Cells(i, 1).Value & "#"
- Set rsx = New ADODB.Recordset
- rsx.Open SQL, cnn, adOpenKeyset, adLockOptimistic
- If rsx.RecordCount = 0 Then
- rsx.AddNew
- For j = 1 To rsx.Fields.Count - 1
- rsx.Fields(Cells(1, j).Value) = Cells(i, j).Value
- Next j
- rsx.Update
- Else
- For j = 2 To rsx.Fields.Count
- rsx.Fields(j - 1) = Cells(i, j - 1).Value
- Next j
- rsx.Update
- End If
- Next i
- rs.Close
- cnn.Close
- Set rs = Nothing
- Set rsx = Nothing
- Set cnn = Nothing
- End Sub
复制代码 |
|