H
Hardhit
Hello All,
I have a query in which I use a function to calculate the quantity that
needs to be ordered based on the stock and the demands of a customer.
The query run's fine the first time I start the database but when I scroll
in the query window I get different quantities that are displayed in the
column where the function is used. Also when I close the query and run it
again I get wrong restuls.
See here the function that I have written.
Option Compare Database
Dim dDifference As Double
Dim strOldPn As String
Function OrderQtyCalc(strPartNr As String, dOrderQty As Double, dPackSize As
Double)
Dim dBoxes As Double
Dim dCalc As Double
If strOldPn = strPartNr Then
dCalc = dDifference - dOrderQty
If dCalc < 0 Then
dBoxes = Round((-dCalc / dPackSize) + 0.5, 0)
OrderQtyCalc = dBoxes * dPackSize
Else
dBoxes = 0
OrderQtyCalc = 0
End If
dDifference = dDifference + (dBoxes * dPackSize) - dOrderQty
Else
dDifference = 0
strOldPn = strPartNr
dCalc = dDifference - dOrderQty
If dCalc < 0 Then
dBoxes = Round((-dCalc / dPackSize) + 0.5, 0)
OrderQtyCalc = dBoxes * dPackSize
Else
dBoxes = 0
OrderQtyCalc = 0
End If
dDifference = dDifference + (dBoxes * dPackSize) - dOrderQty
End If
End Function
And this is the SQL of the query.
SELECT Query1.PoNr, Query1.SupplName, Query1.PartNr, Query1.Date,
OrderQtyCalc([PartNr],[SumOfSumOfOrderQty],[PackQty]) AS OrderQty
FROM Query1
WHERE (((OrderQtyCalc([PartNr],[SumOfSumOfOrderQty],[PackQty]))>0))
ORDER BY Query1.Date;
What is wrong with my code ? I think it has something to do with the public
variable dDifference that I have but I can't get the correct results without
this variable as I need to retain the result of the last calculation for the
next iteration of the query.
How can I get around this problem ?
Regards,
Peter
I have a query in which I use a function to calculate the quantity that
needs to be ordered based on the stock and the demands of a customer.
The query run's fine the first time I start the database but when I scroll
in the query window I get different quantities that are displayed in the
column where the function is used. Also when I close the query and run it
again I get wrong restuls.
See here the function that I have written.
Option Compare Database
Dim dDifference As Double
Dim strOldPn As String
Function OrderQtyCalc(strPartNr As String, dOrderQty As Double, dPackSize As
Double)
Dim dBoxes As Double
Dim dCalc As Double
If strOldPn = strPartNr Then
dCalc = dDifference - dOrderQty
If dCalc < 0 Then
dBoxes = Round((-dCalc / dPackSize) + 0.5, 0)
OrderQtyCalc = dBoxes * dPackSize
Else
dBoxes = 0
OrderQtyCalc = 0
End If
dDifference = dDifference + (dBoxes * dPackSize) - dOrderQty
Else
dDifference = 0
strOldPn = strPartNr
dCalc = dDifference - dOrderQty
If dCalc < 0 Then
dBoxes = Round((-dCalc / dPackSize) + 0.5, 0)
OrderQtyCalc = dBoxes * dPackSize
Else
dBoxes = 0
OrderQtyCalc = 0
End If
dDifference = dDifference + (dBoxes * dPackSize) - dOrderQty
End If
End Function
And this is the SQL of the query.
SELECT Query1.PoNr, Query1.SupplName, Query1.PartNr, Query1.Date,
OrderQtyCalc([PartNr],[SumOfSumOfOrderQty],[PackQty]) AS OrderQty
FROM Query1
WHERE (((OrderQtyCalc([PartNr],[SumOfSumOfOrderQty],[PackQty]))>0))
ORDER BY Query1.Date;
What is wrong with my code ? I think it has something to do with the public
variable dDifference that I have but I can't get the correct results without
this variable as I need to retain the result of the last calculation for the
next iteration of the query.
How can I get around this problem ?
Regards,
Peter