I need some guidance in making a code a little more efficient.
Public Function fncVAT(ByVal tDate As Variant) As Variant
Dim strTDate As String
Dim strVat As String
tDate = CDate(Nz(tDate, 0))
strTDate = "#" & Format(tDate, "mm/dd/yyyy") & "#"
strVat = "SELECT TOP 1 Vat FROM Tax WHERE [EffectiveDate] <= " & strTDate & _
" ORDER BY [EffectiveDate] DESC;"
With CurrentDb.OpenRecordset(strVat)
If Not (.BOF And .EOF) Then
fncVAT = .Fields(0)
End If
End With
End Function
What it does, it applies the current VAT rate from the Tax Table based on the date the product is purchased. For example
Tax Table
+----------------+-------+
| Effective Date | VAT |
+----------------+-------+
| 9/1/17 | 15% |
| 2/1/19 | 12.5% |
+----------------+-------+
Purchases Query
+----------+---------+------+---------+---------------------------------------------------+
| PurDate | Product | Cost | Vatable | Vat |
+----------+---------+------+---------+---------------------------------------------------+
| 1/31/18 | Cola | 70 | 0 | IIf([Vatable]=0,[Cost]*fncVAT([PurDate]),0)=10.50 |
| 12/28/19 | Cola | 70 | 0 | IIf([Vatable]=0,[Cost]*fncVAT([PurDate]),0)=8.75 |
| 5/3/20 | Flour | 15 | -1 | IIf([Vatable]=0,[Cost]*fncVAT([PurDate]),0)=0 |
+----------+---------+------+---------+---------------------------------------------------+
I've narrowed it down to this function. The query has about 900 records and it takes a while to load. Any suggestions? Thanks in advance.
EffectiveDateand/or create one query that incorporates the functionality offncVAT.EffectiveDatebut no change still takes long to load.