Forum Discussion
DateDIFF and a Where Clause
- Oct 08, 2024
Tony2021 , you replace:
..
Abs(DateDiff("d",[qryDrawsDecliningCumDrawn].[FundingDate],[LastBdMo])) AS DayCountDD
with:
Abs(DateDiff("d",[qryDrawsDecliningCumDrawn].[FundingDate], fncLastBusinessDayThisMo([qryDrawsDecliningCumDrawn].[FundingDate])) As DayCountDD
Also change the function to this:
Public Function fncLastBusinessDayThisMo(ByVal FundingDate As Date) As Date Static dte_static As Date Static dte_ans As Date Dim dte As Date dte = DateSerial(Year([FundingDate]), Month([FundingDate]) + 1, 0) If dte = dte_static Then fncLastBusinessDayThisMo = dte_ans Exit Function End If dte_static = dte Do Until InStr(1, "Sat/Sun", Format$(dte, "ddd")) = 0 dte = DateAdd("d", -1, dte) Loop dte_ans = dte fncLastBusinessDayThisMo = dte End Function
thats amazing Arnel.
I am not sure if it would be better to to use the query instead of the table name. The tbldDraws.FundingDate is in tblDraws, the LastBdMo is a function and tblFacility.TypeIDfk is in tblFacility and this is all combined into a query qryA (not the real name...just for simplicity).
You can see my error below. I know the red box is not correct since I am using tblDraws as teh table.
I guess the first question is whether its better to use the name of the query instead of a table name.
I hope this makes sense. Let me know. thank you
Tony2021 , if you can show us the query string (SQL) of query qryA, then i am sure we can simplify the query to add the CountDay calculated column.
- Tony2021Oct 08, 2024Iron Contributor
Hi Arnel / Ken
QryA is as follows. Its long I know. I will try to format it for better readability.
I hope it makes sense.
Just to recap, I am needing the day count between tblDraws.FundingDate and LastBdMo (the last bus day of the month. Just weekday. Not considering holidays) where tblDraws.Type = tblDraws.Type (this is an ID) and I think I will need to add a date condition as well: where tblDraws.[FundingDate] < tblDraws.[FundingDate]
I dont know if its better to insert the codes using the button or just simply pasting. I am using both.
QryA:
SELECT
qryDrawsDecliningCumDrawn.FundingDate, tblFacility.Commitment, [tblfacility].[commitment]-Format(Nz(DSum("SumOfAmount","qryDrawsDecliningCumDrawn","ID=" & [qryDrawsDecliningCumDrawn].[ID] & " And [FirstOfIDDetailpk] < " & Nz([qryDrawsDecliningCumDrawn].[FirstOfIDDetailpk],0)),0),"Currency") AS OpenBal, qryDrawsDecliningCumDrawn.SumOfAmount AS SumDrawn, [OpenBal]-[SumDrawn] AS EndBalAvail, [Commitment]-[EndBalAvail] AS CumDrawn, qryDrawsDecliningCumDrawn.Type, qryDrawsDecliningCumDrawn.ID, qryDrawsDecliningCumDrawn.DrawsIDfk, [sumdrawn]*[AllInRateDD]*[DayCountDD]/360 AS FeeDD, qryInterest.AllINRateDD, Abs(DateDiff("d",[qryDrawsDecliningCumDrawn].[FundingDate],[LastBdMo])) AS DayCountDD, fncLastBusinessDayThisMo([qryDrawsDecliningCumDrawn].[FundingDate]) AS LastBdMo, fncLastBusinessDay([qryDrawsDecliningCumDrawn].[FundingDate]) AS LastBdNxtMo, qryInterest.CLBLiborRate, qryInterest.AllInRateCLB, qryDrawsDecliningCumDrawn.TypeIDfk
FROMqryInterest RIGHT JOIN ((tblDraws INNER JOIN qryDrawsDecliningCumDrawn ON tblDraws.ID = qryDrawsDecliningCumDrawn.ID) INNER JOIN tblFacility ON qryDrawsDecliningCumDrawn.FacIDfk = tblFacility.ID) ON qryInterest.DrawIDfk = qryDrawsDecliningCumDrawn.DrawsIDfk
ORDER BYqryDrawsDecliningCumDrawn.FundingDate, qryDrawsDecliningCumDrawn.TypeIDfk;
this is what it looks like:
qryDrawsDecliningCumDrawn:
SELECT tblDrawsDetails.DrawsIDfk, Sum(tblDrawsDetails.Amount) AS SumOfAmount, tblDrawTypeDropBox.Type, tblFacility.ProjID, tblDrawTypeDropBox.ID, tblFacility.TypeIDfk, tblDrawsDetails.FacIDfk, First(tblDrawsDetails.IDDetailpk) AS FirstOfIDDetailpk, tblDraws.FundingDate, Year([FundingDate])*12+DatePart('m',[FundingDate])-1 AS MonthYrSort FROM tblDraws INNER JOIN ((tblDrawsDetails LEFT JOIN tblFacility ON tblDrawsDetails.FacIDfk = tblFacility.ID) LEFT JOIN tblDrawTypeDropBox ON tblFacility.TypeIDfk = tblDrawTypeDropBox.ID) ON tblDraws.ID = tblDrawsDetails.DrawsIDfk GROUP BY tblDrawsDetails.DrawsIDfk, tblDrawTypeDropBox.Type, tblFacility.ProjID, tblDrawTypeDropBox.ID, tblFacility.TypeIDfk, tblDrawsDetails.FacIDfk, tblDraws.FundingDate, Year([FundingDate])*12+DatePart('m',[FundingDate])-1, tblDraws.Type HAVING (((tblDraws.Type)<>4));
below is the function
fncLastBusinessDayThisMo
' arnelgp - expert on ms tech Public Function fncLastBusinessDayThisMo(ByVal FundingDate As Date) As Date Dim dte As Date dte = DateSerial(Year([FundingDate]), Month([FundingDate]) + 1, 0) Do Until InStr(1, "Sat/Sun", Format$(dte, "ddd")) = 0 dte = DateAdd("d", -1, dte) Loop fncLastBusinessDayThisMo = dte End Function
- arnel_gpOct 08, 2024Iron Contributor
Tony2021 , you replace:
..
Abs(DateDiff("d",[qryDrawsDecliningCumDrawn].[FundingDate],[LastBdMo])) AS DayCountDD
with:
Abs(DateDiff("d",[qryDrawsDecliningCumDrawn].[FundingDate], fncLastBusinessDayThisMo([qryDrawsDecliningCumDrawn].[FundingDate])) As DayCountDD
Also change the function to this:
Public Function fncLastBusinessDayThisMo(ByVal FundingDate As Date) As Date Static dte_static As Date Static dte_ans As Date Dim dte As Date dte = DateSerial(Year([FundingDate]), Month([FundingDate]) + 1, 0) If dte = dte_static Then fncLastBusinessDayThisMo = dte_ans Exit Function End If dte_static = dte Do Until InStr(1, "Sat/Sun", Format$(dte, "ddd")) = 0 dte = DateAdd("d", -1, dte) Loop dte_ans = dte fncLastBusinessDayThisMo = dte End Function