As the DelivDate column must be of text data type rather than date/time to
cater for the "BackOrd" value in the first row then you can't use a simple
subquery correlated on the dates. You'll need to handle it in code by
wrapping everything up in a function. The following should do it:
Function GetOrderQtyCalc(lngPartNr As Long, _
intOrderQty As Integer, _
strDelivDate As String, _
varPendQty As Variant)
Dim strCriteria As String
Dim dtmPrecDate As Date
If strDelivDate = "BackOrd" Then
GetOrderQtyCalc = intOrderQty - varPendQty
Else
strCriteria = "PartNr = " & lngPartNr & _
" And CDate(IIF(IsDate(DelivDate),DelivDate,0)) < #" & _
Format(CDate(strDelivDate), "mm/dd/yyyy") & "#"
dtmPrecDate = DMax("CDate(IIF(IsDate(DelivDate),DelivDate,0))", _
"YourOrderTable", strCriteria)
If dtmPrecDate = 0 Then
strCriteria = "PartNr = " & lngPartNr & _
" And DelivDate = ""BackOrd"""
GetOrderQtyCalc = intOrderQty + DLookup("OrderQty - PendQty", _
"YourOrderTable", strCriteria)
Else
GetOrderQtyCalc = intOrderQty
End If
End If
End Function
Pate this into a standard module and call it in a query like so:
SELECT PartNr, OrderQty, DelivDate, PendQty,
GetOrderQtyCalc(PartNr,OrderQty,DelivDate,PendQty)
AS OrderQtyCalc
FROM YourOrderTable
ORDER BY PartNr,IIf(IsDate(DelivDate),DelivDate,0);
The above does assume that the PendQty form the first row per PartNr is only
carried forward one row, so even if it resulted in a negative OrderQtyCalc in
that row it would not be carried forward to a subsequent row.
Ken Sheridan
Stafford, England
> Hello All,
>
[quoted text clipped - 29 lines]
>
> Peter
Hardhit - 26 Apr 2008 20:23 GMT
Hi Ken,
I tried this code and I keep getting errors. I have replaced in the code the
"YourOrderTable" bij the table involved but it's not working.
I get the Error Datatype mismatch in criteria expression.
The partnumber in the table is not numeric but Txt. I tried changing
lngPartNr as Long to strPartNr as string. I then also changed the variable
in the function so that it is the same as the called variable.
Regards,
> As the DelivDate column must be of text data type rather than date/time to
> cater for the "BackOrd" value in the first row then you can't use a simple
[quoted text clipped - 82 lines]
>>
>> Peter
Ken Sheridan - 27 Apr 2008 21:33 GMT
Peter:
You will get errors if the part number is a text data type. As well as
changing the data type of the argument you need to wrap its values in quotes
characters in the code, like so:
Function GetOrderQtyCalc(strPartNr As String, _
intOrderQty As Integer, _
strDelivDate As String, _
varPendQty As Variant)
Dim strCriteria As String
Dim dtmPrevDate As Date
If strDelivDate = "BackOrd" Then
GetOrderQtyCalc = intOrderQty - varPendQty
Else
strCriteria = "PartNr = """ & strPartNr & _
""" And CDate(IIF(IsDate(DelivDate),DelivDate,0)) < #" & _
Format(CDate(IIf(IsDate(strDelivDate), strDelivDate, 0)),
"mm/dd/yyyy") & "#"
dtmPrevDate = DMax("CDate(IIF(IsDate(DelivDate),DelivDate,0))", _
"YourOrderTable", strCriteria)
If dtmPrevDate = 0 Then
strCriteria = "PartNr = """ & strPartNr & _
""" And DelivDate = ""BackOrd"""
GetOrderQtyCalc = intOrderQty + DLookup("OrderQty - PendQty", _
"YourOrderTable", strCriteria)
Else
GetOrderQtyCalc = intOrderQty
End If
End If
End Function
Ken Sheridan
Stafford, England
> Hi Ken,
>
[quoted text clipped - 95 lines]
> >>
> >> Peter
Hardhit - 29 Apr 2008 22:39 GMT
Thanks Ken,
I fixed up the code as you suggested and I don't get the errors anymore.
Regards,
Peter
> Peter:
>
[quoted text clipped - 145 lines]
>> >>
>> >> Peter