我使用下面的代码在8 [qty]字段中查找值,该值与[make quantity]字段中的值最接近。 如果[qty]字段中的值是升序值,则代码按照我的意愿工作。例如。 10,20,30,40,50,60,70,80
如果值正在降序或混合,则代码将失败,例如。 80,70,60,50,40,30,20,10或10,30,50,40,70,20,80等
任何人都可以建议如何以更好的方式处理这个问题
If Me![Make Quantity] <= Me![qty1] Then
materialprice = Me![raw1]
ElseIf Me![Make Quantity] <= Me![qty2] And Me![Make Quantity] > Me![qty1] Then
materialprice = Me![raw1]
ElseIf Me![Make Quantity] <= Me![qty3] And Me![Make Quantity] > Me![qty2] Then
materialprice = Me![raw2]
ElseIf Me![Make Quantity] <= Me![qty4] And Me![Make Quantity] > Me![qty3] Then
materialprice = Me![raw3]
ElseIf Me![Make Quantity] <= Me![qty5] And Me![Make Quantity] > Me![qty4] Then
materialprice = Me![raw4]
ElseIf Me![Make Quantity] <= Me![qty6] And Me![Make Quantity] > Me![qty5] Then
materialprice = Me![raw5]
ElseIf Me![Make Quantity] <= Me![qty7] And Me![Make Quantity] > Me![qty6] Then
materialprice = Me![raw6]
ElseIf Me![Make Quantity] <= Me![qty8] And Me![Make Quantity] > Me![qty7] Then
materialprice = Me![raw7]
Else
materialprice = Me![raw8]
End If
答案 0 :(得分:2)
已更新!更改了代码,以便在QTY&lt;最低,默认为最低;如果&gt;最高,使用率最高。
这是我把一些代码放在一起的代码...因为我没有你的数据,所以我将测试数据加载到8对文本框中(使用你的字段名称)。请注意,我使用'MakeE'事件为'Make Quantity'文本框调用代码。你可以强制执行...
一旦满意,请注释掉Debug语句和MsgBox。
Option Compare Database
Option Explicit
Private Sub Form_Load()
Dim i As Integer
Dim MakeQuantity As Integer
Dim materialprice As Double
Me.qty1 = 30: Me.qty2 = 20: Me.qty3 = 70: Me.qty4 = 50: Me.qty5 = 10: Me.qty6 = 60: Me.qty7 = 80: Me.qty8 = 70
Me.raw1 = 3.3: Me.raw2 = 2.2: Me.raw3 = 7.7: Me.raw4 = 5.5: Me.raw5 = 1.1: Me.raw6 = 6.6: Me.raw7 = 8.8: Me.raw8 = 7.7
End Sub
Private Sub Make_Quantity_AfterUpdate()
Dim i As Integer
Dim iDiff As Integer
Dim iLow As Integer
Dim iMatch As Integer
Dim iHigh As Integer
Dim dPriceL As Double
Dim dPriceH As Double
Dim dPriceM As Double
Dim iQtyL As Integer
Dim iQtyH As Integer
iLow = 30000
iHigh = 0
iMatch = 10
Debug.Print "Find Qty of: " & Me.[Make Quantity]
For i = 1 To 8
If Int(Me("qty" & i)) <= iLow Then
iLow = Int(Me("qty" & i))
dPriceL = Me("raw" & i)
iQtyL = Int(Me("qty" & i))
End If
If Int(Me("qty" & i)) >= iHigh Then
iHigh = Int(Me("qty" & i))
dPriceH = Me("raw" & i)
iQtyH = Int(Me("qty" & i))
End If
iDiff = Abs(Me.[Make Quantity] - Me("qty" & i))
If Int(Me("qty" & i)) <= Int(Me.[Make Quantity]) Then
If iDiff <= iMatch Then
iMatch = iDiff
dPriceM = Me("raw" & i)
End If
End If
Debug.Print "i: " & i & vbTab & "Qty: " & Me("qty" & i) & vbTab & "Diff: " & iDiff & vbTab & "Raw: " & Me("raw" & i)
Next i
If dPriceM <> 0 Then ' Did we find a suitable match?
MsgBox "Make Quantity: " & Me.[Make Quantity] & vbCrLf & "Price: " & dPriceM
Else ' Didn't find a good match; must be < lowest or > highest
If iQtyL > Int(Me.[Make Quantity]) Then ' Greater than lowest QTY, use Lowest QTY price...
MsgBox "Make Quantity: " & Me.[Make Quantity] & vbCrLf & "Price: " & dPriceL
ElseIf iQtyH < Int(Me.[Make Quantity]) Then ' Greater than highest QTY, use highest QTY price...
MsgBox "Make Quantity: " & Me.[Make Quantity] & vbCrLf & "Price: " & dPriceH
Else
MsgBox "Impossible? Asked for Qty of: " & Me.[Make Quantity] & vbCrLf & _
"Lowest Qty: " & iQtyL & vbTab & _
"Highest Qty: " & iQtyH
End If
End If
End Sub