|
5#
楼主 |
发表于 2014-3-21 10:51:46
|
只看该作者
- Public Sub test()
- Dim rs As ADODB.Recordset
- Set rs = New ADODB.Recordset
-
-
-
- Dim DN As String 'DN
- Dim Item As String 'Item
-
- Dim s As Integer
- Dim Qty1 As Integer 'Qty1
- Dim Qty2 As Integer 'Qty2
- Dim Qty3, Qty4 As Integer 'Qty3,Qty4
- Dim xiang As Integer
-
- Dim SQL As String
-
- Dim mark
-
- s = 0
- Qty2 = 0
- xiang = 0
-
- rs.Open "TABLE1", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
-
-
- Do Until rs.EOF '设定循环条件:直到记录集的指针移动到空记录时,停止循环
-
- Qty2 = rs.Fields("数量")
-
- If xiang = 0 Then
- xiang = 1
- DN = rs.Fields("订单号")
- Item = rs.Fields("产品")
- Qty3 = rs.Fields("数量")
-
- SQL = "INSERT INTO TABLE2 ([订单号],[产品],[数量],[箱号]) VALUES ('" & DN & "','" & Item & "','" & Qty2 & "','" & xiang & "')"
- DoCmd.SetWarnings False
- DoCmd.RunSQL SQL
- Else
-
- If DN = rs.Fields("订单号") Then '订单号相同时
- Item = rs.Fields("产品")
- Qty4 = Qty3
- Qty3 = Qty3 + rs.Fields("数量")
- If Qty3 >= 10 Then '大于10时
-
- If Qty3 > 20 Then
- s = 0
- Qty3 = Qty3 - 10
- Qty1 = Qty3 \ 10
-
- Qty4 = 10 - Qty4
- SQL = "INSERT INTO TABLE2 ([订单号],[产品],[数量],[箱号]) VALUES ('" & DN & "','" & Item & "','" & Qty4 & "','" & xiang & "')"
- DoCmd.RunSQL SQL
- xiang = xiang + 1
-
- Do
- s = s + 1
- SQL = "INSERT INTO TABLE2 ([订单号],[产品],[数量],[箱号]) VALUES ('" & DN & "','" & Item & "','" & 10 & "','" & xiang & "')"
- DoCmd.RunSQL SQL
- xiang = xiang + 1
- Loop Until s = Qty1
-
- Qty3 = Qty3 Mod 10
-
- Else
- Qty3 = Qty3 - 10
- Qty2 = Qty2 - Qty3
-
- SQL = "INSERT INTO TABLE2 ([订单号],[产品],[数量],[箱号]) VALUES ('" & DN & "','" & Item & "','" & Qty2 & "' ,'" & xiang & "')"
- DoCmd.RunSQL SQL
- xiang = xiang + 1
- mark = "x"
- End If
-
- SQL = "INSERT INTO TABLE2 ([订单号],[产品],[数量],[箱号]) VALUES ('" & DN & "','" & Item & "','" & Qty3 & "','" & xiang & "')"
- DoCmd.RunSQL SQL
-
- Else '小于10时
- SQL = "INSERT INTO TABLE2 ([订单号],[产品],[数量],[箱号]) VALUES ('" & DN & "','" & Item & "','" & Qty2 & "','" & xiang & "')"
- DoCmd.RunSQL SQL
- mark = "y"
- End If
-
- Else '订单号不相同时
- xiang = 1
- DN = rs.Fields("订单号")
- Item = rs.Fields("产品")
- Qty3 = rs.Fields("数量")
-
- If Qty2 > 10 Then
- Qty1 = Qty2 \ 10
- Qty3 = Qty2 Mod 10
-
- Do
- s = s + 1
- SQL = "INSERT INTO TABLE2 ([订单号],[产品],[数量],[箱号]) VALUES ('" & DN & "','" & Item & "','" & 10 & "','" & xiang & "')"
- DoCmd.RunSQL SQL
- xiang = xiang + 1
- Loop Until s = Qty1
-
- SQL = "INSERT INTO TABLE2 ([订单号],[产品],[数量],[箱号]) VALUES ('" & DN & "','" & Item & "','" & Qty3 & "','" & xiang & "')"
- DoCmd.RunSQL SQL
-
- ElseIf Qty2 = 10 Then
- SQL = "INSERT INTO TABLE2 ([订单号],[产品],[数量],[箱号]) VALUES ('" & DN & "','" & Item & "','" & 10 & "','" & xiang & "')"
- DoCmd.RunSQL SQL
-
- Else
- SQL = "INSERT INTO TABLE2 ([订单号],[产品],[数量],[箱号]) VALUES ('" & DN & "','" & Item & "','" & Qty2 & "','" & xiang & "')"
- DoCmd.RunSQL SQL
- End If
- mark = ""
- End If
-
- End If
-
- rs.MoveNext
-
- Loop '循环
-
- If mark = "x" Then
- SQL = "INSERT INTO TABLE2 ([订单号],[产品],[数量],[箱号]) VALUES ('" & DN & "','" & Item & "','" & Qty3 & "','" & xiang & "')"
- DoCmd.RunSQL SQL
- DoCmd.SetWarnings True
- End If
-
- rs.Close '关闭记录集
- Set rs = Nothing '将记录集从内存中清除
- End Sub
复制代码
|
|