Hi I'm new to these forums, so please excuse any errors on my part:
Basically, I have a query that is the Record Source for a report. The main query makes use of 2 sub-queries. In one of the sub-queries, it calculates the date of the next call to a customer based on the last call date and a lead time parameter.
When we were just using DateAdd ("d", lead_time, last_call) the query ran quickly about 4 seconds, but now that we are using a function to calculate the next call date to exclude weekends and holidays (the PlusWorkdays function is shown below), it runs extremely slow - about 3 minutes.
Public Function PlusWorkdays (dteStart As Date, intNumDays As Long) As Date
PlusWorkdays = dteStart
Do While intNumDays > 0
PlusWorkdays = DateAdd("d",1,PlusWorkdays)
If Weekday(PlusWorkdays, vbMonday) <=5 And _
IsNull(DLookup("[Holidate]","tbl_Holidays",_
"[Holidate] = #"& PlusWorkdays & #")) Then
intNumDays = intNumDays - 1
End If
Loop
End Function
This function is called in the sub-query as follows:
format(PlusWorkdays(last_call, lead_time),"dd-mmm-yy") AS next_call
This query is performed for about 50,000 records.
Any help to speed up this query would be greatly appreciated.
Basically, I have a query that is the Record Source for a report. The main query makes use of 2 sub-queries. In one of the sub-queries, it calculates the date of the next call to a customer based on the last call date and a lead time parameter.
When we were just using DateAdd ("d", lead_time, last_call) the query ran quickly about 4 seconds, but now that we are using a function to calculate the next call date to exclude weekends and holidays (the PlusWorkdays function is shown below), it runs extremely slow - about 3 minutes.
Public Function PlusWorkdays (dteStart As Date, intNumDays As Long) As Date
PlusWorkdays = dteStart
Do While intNumDays > 0
PlusWorkdays = DateAdd("d",1,PlusWorkdays)
If Weekday(PlusWorkdays, vbMonday) <=5 And _
IsNull(DLookup("[Holidate]","tbl_Holidays",_
"[Holidate] = #"& PlusWorkdays & #")) Then
intNumDays = intNumDays - 1
End If
Loop
End Function
This function is called in the sub-query as follows:
format(PlusWorkdays(last_call, lead_time),"dd-mmm-yy") AS next_call
This query is performed for about 50,000 records.
Any help to speed up this query would be greatly appreciated.
