jonathanchye
Registered User.
- Local time
- Today, 01:20
- Joined
- Mar 8, 2011
- Messages
- 448
Hi all,
I have a table of Couriers and a subtable of Surcharges linked to the Couriers. The table structure is as below:
tblCourier
-PKCourierID
-fkSurchargeID
-txtName
-bolActive
tblSurcharges
-PKSurchargeID
-dteDateActive (the date surcharge starts)
-dblPercentIncrease
My function currently loops through tblSurcharges and tries to work out the latest surcharge based on todays date. This seems to work but highly inefficient. Can anyone recommend a more elegant solution please?
I have a table of Couriers and a subtable of Surcharges linked to the Couriers. The table structure is as below:
tblCourier
-PKCourierID
-fkSurchargeID
-txtName
-bolActive
tblSurcharges
-PKSurchargeID
-dteDateActive (the date surcharge starts)
-dblPercentIncrease
My function currently loops through tblSurcharges and tries to work out the latest surcharge based on todays date. This seems to work but highly inefficient. Can anyone recommend a more elegant solution please?
Code:
Public Sub CalcFuelPercent()
'' THIS WILL CHECK FOR DATE OF FUEL SURCHARGE BY ORDER DATE
On Error Resume Next
If IsNull(Me.Courier) = False And IsNull(Me.OrderDate) = False Then
'Get PK
Dim PK As Long
PK = DLookup("PKCourierID", "tblCourier", "Courier='" & Me.Courier & "'")
' Go to last record for Courier charges
Dim Max As Long
Max = Nz(DMax("PKSurchargeID", "tblSurcharges", "FKCourier=" & PK & ""), 0)
' only do if record exists
If Max > 0 Then
Dim temp As Date
temp = DLookup("DteDateActive", "tblSurcharges", "PKSurchargeID =" & Max & "")
If temp <= Me.OrderDate Then
' Latest date in table <= today
Me.txtSurchargePercent = Nz(DLookup("DblPercentPercentIncrease", "tblSurcharges", "PKSurchargeID =" & Max & ""), 0)
Else
Do
Max = Max + 1
temp = DLookup("DteDateActive", "tblSurcharges", "PKSurchargeID =" & Max & "")
Loop Until temp <= Me.OrderDate
Me.txtSurchargePercent = Nz(DLookup("DblPercentPercentIncrease", "tblSurcharges", "PKSurchargeID =" & Max & ""), 0)
' Reset Values
End If
Else
'No records for fuel surcharge
Me.txtSurchargePercent.Value = 0
End If
Else
Me.txtSurchargePercent.Value = 0
End If
Me.FuelSurcharge = Nz(Me.Price, 0) * (Nz(Me.txtSurchargePercent, 0))
End Sub