Microsoft Access
--
Questions
--
Followers
Top Experts
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
SELECT a.ClientID, b.FyEnd, Sum(fldAmt) From ClientData a
INNER JOIN FYEnds b ON a.ClientID = b.ClientID WHERE a.EntryDate BETWEEN
Iif(Month(Date())>b.FyEnd,
Iif(Month(Date())>b.FyEnd,
I'm assuming a ClientData table has several clients, multi-year dates, and one or more amount fields which you want summed by client. I just saw I neglected the GROUP BY, and don't need to see the FyEnd value:
SELECT a.ClientID, Sum(fldAmt) From ClientData a
INNER JOIN FYEnds b ON a.ClientID = b.ClientID WHERE a.EntryDate BETWEEN
Iif(Month(Date())>b.FyEnd,
Iif(Month(Date())>b.FyEnd,
GROUP BY a.ClientID;






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
The basis function is DateFYStart. As is, it takes February as FYStart.
You could change that to take the month as a parameter:
<snip>
Public Function DateFYStart(ByVal intYear As Integer, ByVal intMonth As Integer) As Date
Dim datFYstart As Date
Dim intWeekday As Integer
Dim intOffset As Integer
' Nominel start date of fiscal year.
datFYstart = DateSerial(intYear, intMonth, 1)
</snip>
To skip the full week adjustment, just remove that part of the function.
/gustav
Public Function DateFYStart(ByVal intYear As Integer) As Date
Dim datFYstart As Date
Dim intWeekday As Integer
Dim intOffset As Integer
' Nominel start date of fiscal year.
datFYstart = DateSerial(intYear, 2, 1)
intWeekday = WeekDay(datFYstart, vbMonday)
If intWeekday <= 4 Then
' Use Monday before.
intOffset = -intWeekday
Else
' Use Monday next.
intOffset = 7 - intWeekday
End If
' Adjust start date to closest Monday.
datFYstart = DateAdd("d", 1 + intOffset, datFYstart)
DateFYStart = datFYstart
End Function
Public Function DateFYEnd(ByVal intYear As Integer) As Date
Dim datFYend As Date
' Subtract one day from start date of next FY.
datFYend = DateAdd("d", -1, DateFYStart(intYear + 1))
DateFYEnd = datFYend
End Function
Public Function FiscalYear(ByVal datDate As Date) As Integer
Dim intFYyear As Integer
intFYyear = Year(datDate)
If DateDiff("d", datDate, DateFYStart(intFYyear)) > 0 Then
' Fiscal year is before calendar year.
intFYyear = intFYyear - 1
End If
FiscalYear = intFYyear
End Function
Public Function FiscalWeek(ByVal datDate As Date) As Long
Dim lngFiscalWeek As Long
lngFiscalWeek = DateDiff("ww", DateFYStart(FiscalYear(datDate)), datDate, vbMonday) + 1
FiscalWeek = lngFiscalWeek
End Function
I'm trying to incorporate your code into my VBA function, but am getting an error on the WHERE statement. It's a syntax error missing operator.
Public Function YTDAmortizationReturnCalc(ByVal ClientNumber As String, ByVal DateOfData As Date, ByVal YTDAmortizationCalc As String) As Variant
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Set db = CurrentDb
ClientNumber = "ABC0"
DateOfData = "3/31/2007"
If YTDAmortizationCalc <> "" And IsNull(YTDAmortizationCalc) = False Then
Set rs = db.OpenRecordset("SELECT " & YTDAmortizationCalc & " FROM tblNonCalcSpreads INNER JOIN tblEBITDA ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber) WHERE tblEBITDA.ClientNumber = '" & ClientNumber & "' AND tblEBITDA.DateOfData = #" & DateOfData & "#")
YTDAmortizationReturnCalc = rs.Fields(0)
rs.Close
Set rs = Nothing
'db.Close
'Set db = Nothing
Else
If ClientNumber = "ABC0" Then
Set rs = db.OpenRecordset("SELECT a.ClientNumber, DSum(Amortization) From tblEBITDA a INNER JOIN tblAVSFSNIDCalc b ON a.ClientNumber = b.ClientNumber WHERE a.DateOfData BETWEEN Iif(Month(Date())>b.FYE,DateSerial(Year(Date()),b.FYE+1,1),DateSerial(Year(Date())-1,b.FYE+1,1) AND Iif(Month(Date())>b.FYE,DateSerial(Year(Date())+1,b.FYE+1,0),DateSerial(Year(Date()),b.FYE+1,0) GROUP BY a.ClientNumber")
Else
YTDAmortizationReturnCalc = Null
End If
End If
'This will save the value back to the table
strSQL = "Update tblEBITDA set YTDAmortization =" & YTDAmortizationReturnCalc & " WHERE tblEBITDA.ClientNumber = '" & ClientNumber & "' AND tblEBITDA.DateOfData = #" & DateOfData & "#"
CurrentDb.Execute strSQL, dbFailOnError
rs.Close
Set rs = Nothing
End Function

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
I've been playing with this trying to get it to work. I tried cactus data's functions and can't get them quite right. I've also played with GRayL's code.
Let's see if I can clarify more of what I'm looking for to see if we are on the right track. Say a client's FYE is March. If I am entering a DateOfData of 5/31/2008, then I would want the sum of Amortization from April and May. If a client's FYE is December and I'm entering DateOfData of 5/31/2008, then I would want the sum of Amortization from January to May. Does that help clarify anything?
Then change 2 to 3 in this line:
' Nominel start date of fiscal year.
datFYstart = DateSerial(intYear, 3, 1)
Now, function FiscalYear will return the Fiscal Year of any date:
intFY = FiscalYear(<any date>)
Then you can find the dates for FY start and for FY end by the functions DateFYStart and DateFYEnd.
datFYStart = DateFYStart(intFY)
datFYEnd = DateFYEnd(intFY)
These dates you can pass as parameters to a query where you filter on dates:
PARAMETERS FYStart DateTime, FYEnd DateTime;
<sql statement>
WHERE
dateField Between FYStart And FYEnd;
/gustav
/gustav
Public Function DateFYStart( _
ByVal intYear As Integer, _
ByVal intFYMonth As Integer) _
As Date
Dim datFYstart As Date
Dim intWeekday As Integer
Dim intOffset As Integer
' Nominel start date of fiscal year.
datFYstart = DateSerial(intYear, intFYMonth, 1)






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Public Function DateFYStart(ByVal intYear As Integer, ByVal intMonth As Integer) As Date
Dim datFYstart As Date
Dim intWeekday As Integer
Dim intOffset As Integer
'Nominel start date of fiscal year
datFYstart = DateSerial(intYear, intMonth, 1)
intWeekday = Weekday(datFYstart, vbMonday)
If intWeekday <= 4 Then
'Use Monday before
intOffset = -intWeekday
Else
'Use Monday next
intOffset = 7 - intWeekday
End If
'Adjust start date to closest Monday
datFYstart = DateAdd("d", 1 + intOffset, datFYstart)
DateFYStart = datFYstart
End Function
Public Function DateFYEnd(ByVal intYear As Integer, ByVal intMonth As Integer) As Date
Dim datFYend As Date
'Subtract one day from start date of next FY
datFYend = DateAdd("d", -1, DateFYStart(intYear + 1, intMonth))
DateFYEnd = datFYend
End Function
Public Function FiscalYear(ByVal datDate As Date) As Integer
Dim intFYyear As Integer
intFYyear = year(datDate)
If DateDiff("d", datDate, DateFYStart(intFYyear, FYE)) > 0 Then
'Fiscal year is before calendar year
intFYyear = intFYyear - 1
End If
FiscalYear = intFYyear
End Function
Public Function YTDAmortizationReturnCalc(ByVal ClientNumber As String, ByVal DateOfData As Date, ByVal YTDAmortizationCalc As String) As Variant
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Set db = CurrentDb
'Dim YTDAmort As Long
ClientNumber = "ABC0"
DateOfData = "3/31/2007"
If YTDAmortizationCalc <> "" And IsNull(YTDAmortizationCalc) = False Then
strSQL = "SELECT " & YTDAmortizationCalc & " " & _
"FROM tblNonCalcSpreads INNER JOIN tblEBITDA " & _
"ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) " & _
"AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber) " & _
"WHERE (((tblEBITDA.ClientNumber) = '" & ClientNumber & "') " & _
"AND ((tblEBITDA.DateOfData) = #" & DateOfData & "#));"
Set rs = db.OpenRecordset(strSQL)
YTDAmortizationReturnCalc = rs.Fields(0)
rs.Close
Set rs = Nothing
'db.Close
Else
FiscalYear (DateOfData)
If ClientNumber = "ABC0" Then
'YTDAmort = DSum("[Amortization]", "tblEBITDA", "[ClientNumber] = '" & ClientNumber & "'")
'AND [DateOfData] BETWEEN DateFYStart(Year(DateOfData), Clients.FYE) " & _
' ' "AND DateFYEnd(Year(DateOfData))
strSQL = "SELECT Sum(Amortization) as Amort " & _
"FROM ((tblClients INNER JOIN tblNonCalcSpreads ON tblClients.ClientNumber = tblNonCalcSpreads.ClientNumber) " & _
"INNER JOIN tblEBITDA ON tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber " & _
"AND tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) " & _
"WHERE tblEBITDA.DateOfData " & _
"BETWEEN DateFYStart(Year(#" & DateOfData & "#), tblClients.FYE) " & _
"AND DateFYEnd(Year(#" & DateOfData & "#), tblClients.FYE)" & _
"GROUP BY tblEBITDA.ClientNumber));"
'iif(Month(#" & DateOfData & "#)>tblClients.FYE,DateSerial(Year(#" & DateOfData & "#),tblClients.FYE+1,1),DateSerial(Year(#" & DateOfData & "#)-1,tblClients.FYE+1,1) " & _
'"AND iif(Month(#" & DateOfData & "#)>tblClients.FYE,DateSerial(Year(#" & DateOfData & "#)+1,tblClients.FYE+1,0),DateSerial(Year(#" & DateOfData & "#),tblClients.FYE+1,0)) " &
Debug.Print strSQL
Set rs = db.OpenRecordset(strSQL)
YTDAmortizationReturnCalc = rs!Amort
Else
YTDAmortizationReturnCalc = Null
End If
End If
'This will save the value back to the table
strSQL = "UPDATE tblEBITDA " & _
"SET YTDAmortization =" & YTDAmortizationReturnCalc & " " & _
"WHERE (((tblEBITDA.ClientNumber) = '" & ClientNumber & "'_ " & _
"AND ((tblEBITDA.DateOfData) = #" & DateOfData & "#));"
CurrentDb.Execute strSQL, dbFailOnError
rs.Close
Set rs = Nothing
Set db = Nothing
End Function
/gustav
Public Function DateFYStart(ByVal intYear As Integer, ByVal intMonth As Integer) As Date
Dim datFYstart As Date
'Nominel start date of fiscal year
datFYstart = DateSerial(intYear, intMonth, 1)
DateFYStart = datFYstart
End Function

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
FYEnd <-> FYE
or vice versa.
/gustav
Public Function YTDAmortizationReturnCalc(ByVal ClientNumber As String, ByVal DateOfData As Date, ByVal YTDAmortizationCalc As String) As Variant
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Set db = CurrentDb
'Dim FYEnd As Date
'Dim YTDAmort As Long
ClientNumber = "ABC0"
DateOfData = "3/31/2007"
If YTDAmortizationCalc <> "" And IsNull(YTDAmortizationCalc) = False Then
strSQL = "SELECT " & YTDAmortizationCalc & " " & _
"FROM tblNonCalcSpreads INNER JOIN tblEBITDA " & _
"ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) " & _
"AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber) " & _
"WHERE (((tblEBITDA.ClientNumber) = '" & ClientNumber & "') " & _
"AND ((tblEBITDA.DateOfData) = #" & DateOfData & "#));"
Set rs = db.OpenRecordset(strSQL)
YTDAmortizationReturnCalc = rs.Fields(0)
rs.Close
Set rs = Nothing
'db.Close
Else
'FYEnd = DateSerial(FiscalYear(DateOfData), [tblClients.FYE], 1)
'Debug.Print FYEnd
If ClientNumber = "ABC0" Then
'YTDAmort = DSum("[Amortization]", "tblEBITDA", "[ClientNumber] = '" & ClientNumber & "'")
'AND [DateOfData] BETWEEN DateFYStart(Year(DateOfData), Clients.FYE) " & _
' ' "AND DateFYEnd(Year(DateOfData))
strSQL = "SELECT Sum(Amortization) as Amort, DateSerial(FiscalYear(DateOfData),tblClients.FYE,1) as FYEnd " & _
"FROM ((tblClients INNER JOIN tblNonCalcSpreads ON tblClients.ClientNumber = tblNonCalcSpreads.ClientNumber) " & _
"INNER JOIN tblEBITDA ON tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber " & _
"AND tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) " & _
"WHERE tblEBITDA.DateOfData " & _
"BETWEEN iif(DateOfData>FYEnd,DateAdd('m', +1, (DateAdd('y', -1, FYEnd)), DateAdd('m', +1, FYEnd))" & _
"AND (DateOfData)" & _
"GROUP BY tblEBITDA.ClientNumber));"
'DateFYStart(Year(#" & DateOfData & "#), tblClients.FYE) " & _
'"AND DateFYEnd(Year(#" & DateOfData & "#), tblClients.FYE)" & _
Debug.Print strSQL
Set rs = db.OpenRecordset(strSQL)
YTDAmortizationReturnCalc = rs!Amort
Else
YTDAmortizationReturnCalc = Null
End If
End If
'This will save the value back to the table
strSQL = "UPDATE tblEBITDA " & _
"SET YTDAmortization =" & YTDAmortizationReturnCalc & " " & _
"WHERE (((tblEBITDA.ClientNumber) = '" & ClientNumber & "'_ " & _
"AND ((tblEBITDA.DateOfData) = #" & DateOfData & "#));"
CurrentDb.Execute strSQL, dbFailOnError
rs.Close
Set rs = Nothing
Set db = Nothing
End Function






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Dim strDate As String
<snip>
DateOfData = #3/31/2007#
strDate = Format(DateOfData, "d\/m\/yyyy hh:nn AM/PM"
If YTDAmortizationCalc <> "" And IsNull(YTDAmortizationCalc) = False Then
strSQL = "SELECT " & YTDAmortizationCalc & " " & _
"FROM tblNonCalcSpreads INNER JOIN tblEBITDA " & _
"ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) " & _
"AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber) " & _
"WHERE (((tblEBITDA.ClientNumber) = '" & ClientNumber & "') " & _
"AND ((tblEBITDA.DateOfData) = #" & strDate & "#));"
<snip>
strSQL = "SELECT Sum(Amortization) as Amort, DateSerial(FiscalYear(DateOfData),tblClients.FYE,1) as FYEnd " & _
"FROM ((tblClients INNER JOIN tblNonCalcSpreads ON tblClients.ClientNumber = tblNonCalcSpreads.ClientNumber) " & _
"INNER JOIN tblEBITDA ON tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber " & _
"AND tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) " & _
"WHERE tblEBITDA.DateOfData " & _
"BETWEEN iif(DateOfData>FYEnd,DateAdd('m', +1, (DateAdd('y', -1, FYEnd)), DateAdd('m', +1, FYEnd))" & _
"AND (DateOfData)" & _
"GROUP BY tblEBITDA.ClientNumber));"
BTW Do you really mean DateSerial(FiscalYear(Date
--------------------------

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
> I have a field in a table that states the month each client uses as their fiscal year end month.
> How would I reference this for calculations such as summing NetChargeoffs for the
> current fiscal year?
The easiest method is to shift the transactional dates to be relative not to the calendar year start but to the fiscal year start. Regarding the fiscal year start as a pseudo 1th. of Jan. makes all other date handling elementary.
Alternatively, establish fiscal year start and fiscal year end and compare transaction dates to these.
/gustav
SELECT a.ClientID, Sum(fldAmt) From ClientData a
INNER JOIN FYEnds b ON a.ClientID = b.ClientID WHERE a.EntryDate BETWEEN
Iif(Month(Date())>b.FyEnd,
Iif(Month(Date())>b.FyEnd,
And a.EntryDate<=DateAdd("d",-
GROUP BY a.ClientID;
I'm getting a syntax error (missing operator) error on the WHERE clause.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
see sql below
Public Function YTDAmortizationReturnCalc(ByVal ClientNumber As String, ByVal DateOfData As Date, ByVal YTDAmortizationCalc As String) As Variant
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Set db = CurrentDb
ClientNumber = "ABC0"
DateOfData = "3/31/2007"
If YTDAmortizationCalc <> "" And IsNull(YTDAmortizationCalc) = False Then
strSQL = "SELECT " & YTDAmortizationCalc & " " & _
"FROM tblNonCalcSpreads INNER JOIN tblEBITDA " & _
"ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) " & _
"AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber) " & _
"WHERE (((tblEBITDA.ClientNumber) = '" & ClientNumber & "') " & _
"AND ((tblEBITDA.DateOfData) = #" & DateOfData & "#));"
Set rs = db.OpenRecordset(strSQL)
YTDAmortizationReturnCalc = rs.Fields(0)
rs.Close
Set rs = Nothing
'db.Close
Else
If ClientNumber = "ABC0" Then
strSQL = "SELECT tblEBITDA.ClientNumber, Sum(tblEBITDA.Amortization) " & _
"FROM tblEBITDA INNER JOIN tblClients " & _
"ON tblEBITDA.ClientNumber = tblClients.ClientNumber " & _
"WHERE tblEBITDA.DateOfData BETWEEN " & _
"Iif(Month(Date())>tblClients.FYE,DateSerial(Year(Date()),tblClients.FYE+1,1),DateSerial(Year(Date())-1,tblClients.FYE+1,1) " & _
"AND Iif(Month(Date())>tblClients.FYE,DateSerial(Year(Date())+1,tblClients.FYE+1,0),DateSerial(Year(Date()),tblClients.FYE+1,0) " & _
"AND tblEBITDA.DateOfData<=DateAdd('d',-Day(Date()),Date())" & _
"GROUP BY tblEBITDA.ClientNumber;"
Debug.Print strSQL
Set rs = db.OpenRecordset(strSQL)
YTDAmortizationReturnCalc = rs!Amort
Else
YTDAmortizationReturnCalc = Null
End If
End If
'This will save the value back to the table
strSQL = "UPDATE tblEBITDA " & _
"SET YTDAmortization =" & YTDAmortizationReturnCalc & " " & _
"WHERE (((tblEBITDA.ClientNumber) = '" & ClientNumber & "'_ " & _
"AND ((tblEBITDA.DateOfData) = #" & DateOfData & "#));"
CurrentDb.Execute strSQL, dbFailOnError
rs.Close
Set rs = Nothing
Set db = Nothing
End Function
I think your first If statement could be replaced by:
If Nz(YTDAmortizationCalc,"")

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
SELECT tblEBITDA.ClientNumber, Sum(tblNonCalcSpreads.Amortization) AS Amort, tblEBITDA.DateOfData
FROM (tblClients INNER JOIN tblEBITDA ON tblClients.ClientNumber = tblEBITDA.ClientNumber) INNER JOIN tblNonCalcSpreads ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber) AND (tblClients.ClientNumber = tblNonCalcSpreads.ClientNumber)
GROUP BY tblEBITDA.ClientNumber
HAVING (((tblEBITDA.DateOfData) Between IIf(Month(Date())>[tblClients].[FYE],DateSerial(Year(Date())+1,[tblClients].[FYE],0),DateSerial(Year(Date()),[tblClients].[FYE]+1,0)) And IIf(Month(Date())>[tblClients].[FYE],DateSerial(Year(Date())+1,[tblClients].[FYE]+1,0),DateSerial(Year(Date()),[tblClients].[FYE]+1,0))) AND ((tblEBITDA.DateOfData)<=DateAdd('d',-Day(Date()),Date())));






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
SELECT tblEBITDA.ClientNumber, Sum(tblNonCalcSpreads.Amortization) AS Amort
FROM (tblClients
INNER JOIN tblEBITDA ON tblClients.ClientNumber = tblEBITDA.ClientNumber)
INNER JOIN tblNonCalcSpreads ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData)
AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber)
AND (tblClients.ClientNumber = tblNonCalcSpreads.ClientNumber)
WHERE tblEBITDA.DateOfData Between IIf(Month(Date())>[tblClients].[FYE],
DateSerial(Year(Date()),[tblClients].[FYE]+1,1),DateSerial(Year(Date()),[tblClients].[FYE]+1,0)) AND
IIf(Month(Date())>[tblClients].[FYE],
DateSerial(Year(Date())+1,[tblClients].[FYE]+1,0),DateSerial(Year(Date()),[tblClients].[FYE]+1,0)))
GROUP BY tblEBITDA.ClientNumber
The FYE = 12 and the DateOfData = 3/31/2007, so the YTDDepreciation should be the Depreciation for January + Febraury + March, but it's not giving me any record. I'm not sure why as there is data in the table.
Public Function YTDDepreciationReturnCalc(ByVal ClientNumber As String, ByVal DateOfData As Date, ByVal YTDDepreciationCalc As String) As Variant
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Set db = CurrentDb
ClientNumber = "ABC0"
DateOfData = "3/31/2007"
If YTDDepreciationCalc <> "" And IsNull(YTDDepreciationCalc) = False Then
strSQL = "SELECT " & YTDDepreciationCalc & " " & _
"FROM tblNonCalcSpreads INNER JOIN tblEBITDA " & _
"ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) " & _
"AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber) " & _
"WHERE tblEBITDA.ClientNumber = '" & ClientNumber & "' " & _
"AND tblEBITDA.DateOfData = #" & DateOfData & "#"
Set rs = db.OpenRecordset(strSQL)
YTDDepreciationReturnCalc = rs.Fields(0)
rs.Close
Set rs = Nothing
Else
If ClientNumber = "ABC0" Then
strSQL = "SELECT tblEBITDA.ClientNumber, Sum(tblNonCalcSpreads.Depreciation) AS Depr " & _
"FROM (tblClients INNER JOIN tblEBITDA " & _
"ON tblClients.ClientNumber = tblEBITDA.ClientNumber) INNER JOIN tblNonCalcSpreads " & _
"ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) " & _
"AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber) " & _
"AND (tblClients.ClientNumber = tblNonCalcSpreads.ClientNumber) " & _
"WHERE (((tblEBITDA.DateOfData) BETWEEN " & _
"IIf(Month(Date())>[tblClients].[FYE],DateSerial(Year(Date())+1,[tblClients].[FYE],0),DateSerial(Year(Date()),[tblClients].[FYE]+1,0)) " & _
"AND IIf(Month(Date())>[tblClients].[FYE],DateSerial(Year(Date())+1,[tblClients].[FYE]+1,0),DateSerial(Year(Date()),[tblClients].[FYE]+1,0))))" & _
"GROUP BY tblEBITDA.ClientNumber;"
Debug.Print strSQL
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount = 0 Then
Debug.Print rs.RecordCount
YTDDepreciationReturnCalc = Null
Exit Function
End If
YTDDepreciationReturnCalc = rs!Depr
Else
YTDDepreciationReturnCalc = Null
End If
End If
'This will save the value back to the table
strSQL = "UPDATE tblEBITDA " & _
"SET YTDDepreciation =" & YTDDepreciationReturnCalc & " " & _
"WHERE tblEBITDA.ClientNumber = '" & ClientNumber & "' " & _
"AND tblEBITDA.DateOfData = #" & DateOfData & "#"
CurrentDb.Execute strSQL, dbFailOnError
rs.Close
Set rs = Nothing
'db.Close
Set db = Nothing
End Function

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Public Function YTDDepreciationReturnCalc(ByVal CliNum As String, ByVal strDate As Date, ByVal YTDDepreciationCalc As String) As Variant
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Set db = CurrentDb
CliNum = "ABC0"
strDate = "3/31/2007"
If YTDDepreciationCalc <> "" And IsNull(YTDDepreciationCalc) = False Then
strSQL = "SELECT " & YTDDepreciationCalc & " " & _
"FROM tblNonCalcSpreads INNER JOIN tblEBITDA " & _
"ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) " & _
"AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber) " & _
"WHERE tblEBITDA.ClientNumber = '" & CliNum & "' " & _
"AND tblEBITDA.DateOfData = #" & strDate & "#"
Set rs = db.OpenRecordset(strSQL)
YTDDepreciationReturnCalc = rs.Fields(0)
rs.Close
Set rs = Nothing
Else
If CliNum = "ABC0" Then
strSQL = "SELECT tblEBITDA.ClientNumber, Sum(tblNonCalcSpreads.Depreciation) AS Depr " & _
"FROM (tblClients INNER JOIN tblEBITDA " & _
"ON tblClients.ClientNumber = tblEBITDA.ClientNumber) INNER JOIN tblNonCalcSpreads " & _
"ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) " & _
"AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber) " & _
"AND (tblClients.ClientNumber = tblNonCalcSpreads.ClientNumber) " & _
"WHERE (((tblEBITDA.DateOfData) BETWEEN " & _
"IIf(Month(Date())>[tblClients].[FYE],DateSerial(Year(Date())+1,[tblClients].[FYE],0),DateSerial(Year(Date()),[tblClients].[FYE]+1,0)) " & _
"AND IIf(Month(Date())>[tblClients].[FYE],DateSerial(Year(Date())+1,[tblClients].[FYE]+1,0),DateSerial(Year(Date()),[tblClients].[FYE]+1,0))))" & _
"GROUP BY tblEBITDA.ClientNumber;"
Debug.Print strSQL
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount = 0 Then
Debug.Print rs.RecordCount
YTDDepreciationReturnCalc = Null
Exit Function
End If
YTDDepreciationReturnCalc = rs!Depr
Else
YTDDepreciationReturnCalc = Null
End If
End If
'This will save the value back to the table
strSQL = "UPDATE tblEBITDA " & _
"SET YTDDepreciation =" & YTDDepreciationReturnCalc & " " & _
"WHERE tblEBITDA.ClientNumber = '" & CliNum & "' " & _
"AND tblEBITDA.DateOfData = #" & strDate & "#"
CurrentDb.Execute strSQL, dbFailOnError
rs.Close
Set rs = Nothing
'db.Close
Set db = Nothing
End Function
Between 2008-07-01 and 2009-06-30
What you wrote will produce 2009-05-31 and 2009 -06-30
Public Function YTDDepreciationReturnCalc(ByVal CliNum As String, ByVal strDate As Date, ByVal YTDDepreciationCalc As String) As Variant
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Set db = CurrentDb
CliNum = "ABC0"
strDate = "12/31/2007"
If YTDDepreciationCalc <> "" And IsNull(YTDDepreciationCalc) = False Then
strSQL = "SELECT " & YTDDepreciationCalc & " " & _
"FROM tblNonCalcSpreads INNER JOIN tblEBITDA " & _
"ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) " & _
"AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber) " & _
"WHERE tblEBITDA.ClientNumber = '" & CliNum & "' " & _
"AND tblEBITDA.DateOfData = #" & strDate & "#"
Set rs = db.OpenRecordset(strSQL)
YTDDepreciationReturnCalc = rs.Fields(0)
rs.Close
Set rs = Nothing
Else
If CliNum = "ABC0" Then
strSQL = "SELECT tblEBITDA.ClientNumber, Sum(tblNonCalcSpreads.Depreciation) AS Depr " & _
"FROM (tblClients INNER JOIN tblEBITDA " & _
"ON tblClients.ClientNumber = tblEBITDA.ClientNumber) INNER JOIN tblNonCalcSpreads " & _
"ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) " & _
"AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber) " & _
"AND (tblClients.ClientNumber = tblNonCalcSpreads.ClientNumber) " & _
"WHERE (((tblEBITDA.DateOfData) BETWEEN " & _
"IIf(Month(Date())>[tblClients].[FYE],DateSerial(Year(Date()),[tblClients].[FYE]+1,1),DateSerial(Year(Date()),[tblClients].[FYE]+1,1)) " & _
"AND IIf(Month(Date())>[tblClients].[FYE],DateSerial(Year(Date())+1,[tblClients].[FYE]+1,0),DateSerial(Year(Date()),[tblClients].[FYE]+1,0))))" & _
"GROUP BY tblEBITDA.ClientNumber;"
Debug.Print strSQL
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount = 0 Then
Debug.Print rs.RecordCount
YTDDepreciationReturnCalc = Null
Exit Function
End If
YTDDepreciationReturnCalc = rs!Depr
Else
YTDDepreciationReturnCalc = Null
End If
End If
'This will save the value back to the table
strSQL = "UPDATE tblEBITDA " & _
"SET YTDDepreciation =" & YTDDepreciationReturnCalc & " " & _
"WHERE tblEBITDA.ClientNumber = '" & CliNum & "' " & _
"AND tblEBITDA.DateOfData = #" & strDate & "#"
CurrentDb.Execute strSQL, dbFailOnError
rs.Close
Set rs = Nothing
'db.Close
Set db = Nothing
End Function






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
I think we are at the point where you have to strip the mdb down to the min required to support the question and upload it using Attach File below. Be sure do a Compact and Repair before uploading.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
? YTDDepreciationReturnCalc(

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Public Function YTDDepreciationReturnCalc(ByVal CliNum As String, ByVal strDate As String, ByVal YTDDepreciationCalc As String) As Variant
Comment out line 7 - prefix a single quote.Now when I call the procedure:
? ytddepreciationreturncalc(
14
Is that what you expect?






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Comment out line 7 - prefix a single quote.
The is the line where I'm setting strDate, correct?

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Line 7 is prefixed with a single quote - commented out - thus ignored by the compiler. I also commented out line 55
using the actual field name
? ytddepreciationreturncalc(
14
The function as I used it is below
Public Function YTDDepreciationReturnCalc(ByVal CliNum As String, ByVal strDate As String, ByVal YTDDepreciationCalc As String) As Variant
Dim db As dao.Database
Dim rs As dao.Recordset
Dim strSQL As String
Set db = CurrentDb
CliNum = "ABC0"
'strDate = "12/31/2007"
If YTDDepreciationCalc <> "" And IsNull(YTDDepreciationCalc) = False Then
strSQL = "SELECT " & YTDDepreciationCalc & " " & _
"FROM tblNonCalcSpreads INNER JOIN tblEBITDA " & _
"ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) " & _
"AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber) " & _
"WHERE tblEBITDA.ClientNumber = '" & CliNum & "' " & _
"AND tblEBITDA.DateOfData = #" & strDate & "#"
Set rs = db.OpenRecordset(strSQL)
YTDDepreciationReturnCalc = rs.Fields(0)
rs.Close
Set rs = Nothing
Else
If CliNum = "ABC0" Then
strSQL = "SELECT tblEBITDA.ClientNumber, Sum(tblNonCalcSpreads.Depreciation) AS Depr " & _
"FROM (tblClients INNER JOIN tblEBITDA " & _
"ON tblClients.ClientNumber = tblEBITDA.ClientNumber) INNER JOIN tblNonCalcSpreads " & _
"ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) " & _
"AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber) " & _
"AND (tblClients.ClientNumber = tblNonCalcSpreads.ClientNumber) " & _
"WHERE (((tblEBITDA.DateOfData) BETWEEN " & _
"IIf(Month(Date())>[tblClients].[FYE],DateSerial(Year(Date()),[tblClients].[FYE]+1,1),DateSerial(Year(Date()),[tblClients].[FYE]+1,1)) " & _
"AND IIf(Month(Date())>[tblClients].[FYE],DateSerial(Year(Date())+1,[tblClients].[FYE]+1,0),DateSerial(Year(Date()),[tblClients].[FYE]+1,0))))" & _
"GROUP BY tblEBITDA.ClientNumber;"
Debug.Print strSQL
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount = 0 Then
Debug.Print rs.RecordCount
YTDDepreciationReturnCalc = Null
Exit Function
End If
YTDDepreciationReturnCalc = rs!Depr
Else
YTDDepreciationReturnCalc = Null
End If
End If
'This will save the value back to the table
strSQL = "UPDATE tblEBITDA " & _
"SET YTDDepreciation =" & YTDDepreciationReturnCalc & " " & _
"WHERE tblEBITDA.ClientNumber = '" & CliNum & "' " & _
"AND tblEBITDA.DateOfData = #" & strDate & "#"
CurrentDb.Execute strSQL, dbFailOnError
'rs.Close
Set rs = Nothing
'db.Close
Set db = Nothing
End Function






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
ClientNumber DateOfData YTDDepreciation
ABC0 1/31/2007 5
ABC0 2/28/2007 9
ABC0 3/31/2007 52
ABC0 4/30/2007 19
ABC0 5/31/2007 23
ABC0 6/30/2007 28
ABC0 7/31/2007 33
ABC0 8/31/2007 37
ABC0 9/30/2007 42
ABC0 10/31/2007 45
ABC0 11/30/2007 49
ABC0 12/31/2007 52
tblEBITDAtblEBITDAYTDDepreciation59141923283
Note the third record is 14. I'm guessing 52 in your copy?
tblEBITDA
YTDDepreciation
5
9
14
19
23
28
33
37
42
45
49
52

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
SELECT tblNonCalcSpreads.ClientNumber, tblNonCalcSpreads.DateOfData, YTDDepreciationReturnCalc([tblNonCalcSpreads].[ClientNumber],[tblNonCalcSpreads].[DateOfData],[tblEBITDA].[YTDDepreciation]) AS YTDDepreciation
FROM (tblClients INNER JOIN tblNonCalcSpreads ON tblClients.ClientNumber = tblNonCalcSpreads.ClientNumber) INNER JOIN (tblEBITDACalc INNER JOIN tblEBITDA ON tblEBITDACalc.ClientNumber = tblEBITDA.ClientNumber) ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber)
WHERE (((tblNonCalcSpreads.ClientNumber)="ABC0"));

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
YTDDepreciation
5
9
52 <- notice this. As I said in my post at http:#a21963861
19
23
28
33
37
42
45
49
52






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
The third variable is defined in the table as long integer.
Getting to the function, that arguement is received as ByVal YTDDepreciationCalc As String
You then go on to build a query: "SELECT " & YTDDepreciationCalc & etc. I cannot see what tblNonCalcSpreads contributes to the query.
Insert: Debug.Print strSQL :at line 1892 of your EDITDACalcFields module. Run qryCalcTest.
You now have a recordset of 27 rows of whatever was the value of tblEBITDA.YTDDepreciation for that month.
Is that what you expected. It doesn't make sense to me.
tblNonCalcSpreads is where the values for Depreciation are, so that is referenced to pull that data.
Is that what I expected? Yes and No. I want the YTDDepreciation displayed, but not pulled directly from tblEBITDA. I want it to be calculated as some of the values may have changed.
I've adjusted the function using your code to reference the DateOfData value as that is what should be compared to the FYE. There's something not quite right as I'm getting 3 as a result no matter what DateOfData I use.
Does this make any sense?
Public Function YTDDepreciationReturnCalc(ByVal CliNum As String, ByVal strDate As Date, ByVal YTDDepreciationCalc As String) As Variant
Dim db As dao.Database
Dim rs As dao.Recordset
Dim strSQL As String
Set db = CurrentDb
CliNum = "ABC0"
strDate = "5/31/2007"
If YTDDepreciationCalc <> "" And IsNull(YTDDepreciationCalc) = False Then
strSQL = "SELECT " & YTDDepreciationCalc & " " & _
"FROM tblNonCalcSpreads INNER JOIN tblEBITDA " & _
"ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) " & _
"AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber) " & _
"WHERE tblEBITDA.ClientNumber = '" & CliNum & "' " & _
"AND tblEBITDA.DateOfData = #" & strDate & "#"
Set rs = db.OpenRecordset(strSQL)
YTDDepreciationReturnCalc = rs.Fields(0)
rs.Close
Set rs = Nothing
Else
If CliNum = "ABC0" Then
strSQL = "SELECT Sum(tblNonCalcSpreads.Depreciation) AS Depr " & _
"FROM (tblClients INNER JOIN tblEBITDA " & _
"ON tblClients.ClientNumber = tblEBITDA.ClientNumber) INNER JOIN tblNonCalcSpreads " & _
"ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) " & _
"AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber) " & _
"AND (tblClients.ClientNumber = tblNonCalcSpreads.ClientNumber) " & _
"WHERE (((tblEBITDA.DateOfData) BETWEEN " & _
"IIf(Month(#" & strDate & "#)>[tblClients].[FYE],DateSerial(Year(#" & strDate & "#),[tblClients].[FYE]+1,1),DateSerial(Year(#" & strDate & "#),[tblClients].[FYE]+1,1)) " & _
"AND IIf(Month(#" & strDate & "#)>[tblClients].[FYE],DateSerial(Year(#" & strDate & "#)+1,[tblClients].[FYE]+1,0),DateSerial(Year(#" & strDate & "#),[tblClients].[FYE]+1,0))));"
Debug.Print strSQL
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount = 0 Then
Debug.Print rs.RecordCount
YTDDepreciationReturnCalc = Null
Exit Function
End If
YTDDepreciationReturnCalc = rs!Depr
Debug.Print rs!Depr
Else
YTDDepreciationReturnCalc = Null
End If
End If
'This will save the value back to the table
strSQL = "UPDATE tblEBITDA " & _
"SET YTDDepreciation =" & YTDDepreciationReturnCalc & " " & _
"WHERE tblEBITDA.ClientNumber = '" & CliNum & "' " & _
"AND tblEBITDA.DateOfData = #" & strDate & "#"
CurrentDb.Execute strSQL, dbFailOnError
'rs.Close
Set rs = Nothing
'db.Close
Set db = Nothing
End Function
I think the psuedo code should be
OldValue = DLookup(YTDDepreciation in tlbEBITDA)
If Sum(tblNonCalcSpreads.Depr
Comments?






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
I have had to submit a complaint about the 'improved' textbox EE installed. It has become so horribly slow I now have to do all my input in NotePad and paste into the box
With my changes I made, I'm getting a result of 3 for any DateOfData I use and it is being updated back to tblEBITDA. I'm just not sure why a value of 3 is returned.
This is the SELECT clause of the query which calls the function. Note the third varilable of the call is [tblEBITDA].[YTDDepreciati

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
The call for the function should be YTDDepreciationReturnCalc(






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Public Function YTDDepreciationReturnCalc(ByVal CliNum As String, ByVal strDate As Date, ByVal YTDDepreciationCalc As String) As Variant
Dim db As dao.Database
Dim rs As dao.Recordset
Dim strSQL As String
Set db = CurrentDb
CliNum = "ABC0"
'strDate = "5/31/2007" <--Commented out
'If YTDDepreciationCalc <> "" And IsNull(YTDDepreciationCalc) = False Then <---Changed as in next line
Nz(YTDDepreciationCalc,"")<> ""
strSQL = "SELECT " & YTDDepreciationCalc & " " & _
"FROM tblNonCalcSpreads INNER JOIN tblEBITDA " & _
"ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) " & _
"AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber) " & _
"WHERE tblEBITDA.ClientNumber = '" & CliNum & "' " & _
"AND tblEBITDA.DateOfData = #" & strDate & "#"
Set rs = db.OpenRecordset(strSQL)
YTDDepreciationReturnCalc = rs.Fields(0)
rs.Close
Set rs = Nothing
Else
If CliNum = "ABC0" Then
strSQL = "SELECT Sum(tblNonCalcSpreads.Depreciation) AS Depr " & _
"FROM (tblClients INNER JOIN tblEBITDA " & _
"ON tblClients.ClientNumber = tblEBITDA.ClientNumber) INNER JOIN tblNonCalcSpreads " & _
"ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) " & _
"AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber) " & _
"AND (tblClients.ClientNumber = tblNonCalcSpreads.ClientNumber) " & _
"WHERE tblEBITDA.DateOfData BETWEEN " & _
"IIf(Month(strDate)>[tblClients].[FYE],DateSerial(Year(strDate),[tblClients].[FYE]+1,1), " & _
"DateSerial(Year(strDate)-1,[tblClients].[FYE]+1,1)) AND " & _
"IIf(Month(strDate)>[tblClients].[FYE],DateSerial(Year(strDate)+1,[tblClients].[FYE]+1,0), " & _
"DateSerial(Year(strDate),[tblClients].[FYE]+1,0));"
Debug.Print strSQL
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount = 0 Then
Debug.Print rs.RecordCount
YTDDepreciationReturnCalc = Null
Exit Function
End If
YTDDepreciationReturnCalc = rs!Depr
Debug.Print rs!Depr
Else
YTDDepreciationReturnCalc = Null
End If
End If
'This will save the value back to the table
strSQL = "UPDATE tblEBITDA " & _
"SET YTDDepreciation =" & YTDDepreciationReturnCalc & " " & _
"WHERE tblEBITDA.ClientNumber = '" & CliNum & "' " & _
"AND tblEBITDA.DateOfData = #" & strDate & "#"
CurrentDb.Execute strSQL, dbFailOnError
'rs.Close
Set rs = Nothing
'db.Close
Set db = Nothing
End Function
I'm getting an error on the strSQL line starting at line 26. Too few parameters. Expected 1.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
see sql below
After another good hard look at this thing, I can see where strSQL in the Else part is totally wrong. The function is passed the ClientNumber as CliNum and the date as strDate so you can use tblNonCalcSpreads directly without any joins.
strSQL = "SELECT tblNonCalcSpreads.ClientNumber, Sum(tblNonCalcSpreads.Depreciation) AS Depr " & _
"FROM tblNonCalcSpreads WHERE tblNonCalcSpreads.DateOfData = #" & strDate & _
"# AND tblNonCalcSpreads.ClientNumber = '" & CliNum & _
"' GROUP BY tblNonCalcSpreads.ClientNumber;"






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
I've worked with both PeopleSoft and Great Plains. And for a while our Fiscal calendar was 6 months off from the actual calendar. The way both applications handle it is that they have the actual date of the entry in the table and then they had individual columns for FY Fiscal_Month and Fiscal_Day as 3 separate columns that were populated when the entry was made in the table(s).
There was a master calendar table that had the beginning and end for a given fiscal year.
While I know it would be a complete redesign, it might be the better solution.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Cheers, Andrew
strDate = "#07/14/2008#"
strDate = "1/31/2007"
When I added the #'s I get a type mismatch error.
strDate = #07/14/2008#






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
You need the double quotes for this as strDate is a string variable.
You need to #'s because in the SQL string your criteria needs to read
WHERE FieldName = #MM/DD/YYYY#
Cheers, Andrew

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
You should also either rename the variable to dtmDate or change it to a String.
Finally if your defaulkt date format is MM/DD/YY or MM/DD/YYYY you should be OK but if it isnt then you may need to FORMAT strDate, each reference to it will need to be
Format(strDate,"MM/DD/YYYY
Cheers, Andrew
"WHERE tblNonCalcSpreads.DateOfData BETWEEN " & _
"IIf(Month(#" & strDate & "#)>[tblClients].[FYE],DateSerial(Year(#" & strDate & "#),[tblClients].[FYE]+1,1), " & _
"DateSerial(Year(#" & strDate & "#)-1,[tblClients].[FYE]+1,1)) AND #" & _
strData & "#));"
When I step throught the code in VBA I get 52 for a result no matter what date I use. When I call the function in a query I get #Error as a result for all dates. This code should work, it doesn't make sense to me why I'm getting no/wrong data.
It is probably also worth creating the SQL as an Access Query and running that then we can look at converting the SQL into VBA if the query is working correctly.
Cheers, Andrew






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Public Function YTDDepreciationReturnCalc(ByVal CliNum As String, ByVal dtDate As Date, ByVal YTDDepreciationCalc As String) As Variant
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Set db = CurrentDb
CliNum = "ABC0"
dtDate = #10/31/2007#
If YTDDepreciationCalc <> "" And IsNull(YTDDepreciationCalc) = False Then
strSQL = "SELECT " & YTDDepreciationCalc & " " & _
"FROM tblNonCalcSpreads INNER JOIN tblEBITDA " & _
"ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) " & _
"AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber) " & _
"WHERE tblEBITDA.ClientNumber = '" & CliNum & "' " & _
"AND tblEBITDA.DateOfData = #" & dtDate & "#"
Set rs = db.OpenRecordset(strSQL)
YTDDepreciationReturnCalc = rs.Fields(0)
rs.Close
Set rs = Nothing
Else
If CliNum = "ABC0" Then
strSQL = "SELECT Sum(tblNonCalcSpreads.Depreciation) AS Depr " & _
"FROM (tblNonCalcSpreads INNER JOIN tblClients " & _
"ON tblNonCalcSpreads.ClientNumber = tblClients.ClientNumber " & _
"WHERE tblNonCalcSpreads.DateOfData BETWEEN " & _
"IIf(Month(#" & dtDate & "#)>[tblClients].[FYE],DateSerial(Year(#" & dtDate & "#),[tblClients].[FYE]+1,1), " & _
"DateSerial(Year(#" & dtDate & "#)-1,[tblClients].[FYE]+1,1)) AND " & _
"IIf(Month(#" & dtDate & "#)>[tblClients].[FYE],DateSerial(Year(#" & dtDate & "#)+1,[tblClients].[FYE]+1,0), " & _
"DateSerial(Year(#" & dtDate & "#),[tblClients].[FYE]+1,0));"
Debug.Print strSQL
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount = 0 Then
Debug.Print rs.RecordCount
YTDDepreciationReturnCalc = Null
Exit Function
End If
YTDDepreciationReturnCalc = rs!Depr
Debug.Print rs!Depr
Else
YTDDepreciationReturnCalc = Null
End If
End If
'This will save the value back to the table
strSQL = "UPDATE tblEBITDA " & _
"SET YTDDepreciation =" & YTDDepreciationReturnCalc & " " & _
"WHERE tblEBITDA.ClientNumber = '" & CliNum & "' " & _
"AND tblEBITDA.DateOfData = #" & dtDate & "#"
CurrentDb.Execute strSQL, dbFailOnError
'rs.Close
Set rs = Nothing
'db.Close
Set db = Nothing
End Function
Secondly can you paste the result of your Debug.Print
Cheers, Andrew
What do you get if you remove the WHERE condition completely from the SQL?
Cheers, Andrew

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
SELECT Sum(tblNonCalcSpreads.Depreciation) AS Depr FROM (tblClients INNER JOIN tblNonCalcSpreads ON tblClients.ClientNumber = tblNonCalcSpreads.ClientNumber) INNER JOIN (tblEBITDACalc INNER JOIN tblEBITDA ON tblEBITDACalc.ClientNumber = tblEBITDA.ClientNumber) ON (tblNonCalcSpreads.DateOfData = tblEBITDA.DateOfData) AND (tblNonCalcSpreads.ClientNumber = tblEBITDA.ClientNumber)WHERE tblNonCalcSpreads.DateOfData BETWEEN IIf(Month(#10/31/2007#)>[tblClients].[FYE],DateSerial(Year(#10/31/2007#),[tblClients].[FYE]+1,1), DateSerial(Year(#10/31/2007#)-1,[tblClients].[FYE]+1,1)) AND IIf(Month(#10/31/2007#)>[tblClients].[FYE],DateSerial(Year(#10/31/2007#)+1,[tblClients].[FYE]+1,0), DateSerial(Year(#10/31/2007#),[tblClients].[FYE]+1,0));
I put the code in a query and added a GROUP BY on ClientNumber and DateOfData. It's pulling the actual value from tblNonCalcSpreads for Depreciation instead of summing them for a YTD number. When I remove the GROUP BY, it sums everything together, which is 52.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Cheers, Andrew
Cheers, Andrew
The first 2 columns are where the month of the date is after the FYE.
BETWEEN - AND BETWEEN - AND
01/02/2007 01/02/2008 01/02/2006 01/02/2007
01/03/2007 01/03/2008 01/03/2006 01/03/2007
01/04/2007 01/04/2008 01/04/2006 01/04/2007
01/05/2007 01/05/2008 01/05/2006 01/05/2007
01/06/2007 01/06/2008 01/06/2006 01/06/2007
01/07/2007 01/07/2008 01/07/2006 01/07/2007
01/08/2007 01/08/2008 01/08/2006 01/08/2007
01/09/2007 01/09/2008 01/09/2006 01/09/2007
01/10/2007 01/10/2008 01/10/2006 01/10/2007
01/11/2007 01/11/2008 01/11/2006 01/11/2007
01/12/2007 01/12/2008 01/12/2006 01/12/2007
01/01/2008 01/01/2009 01/01/2007 01/01/2008
Sub AJ()
Dim cnt As Long
Dim strSQL
For cnt = 1 To 12
Debug.Print DateSerial(Year(#10/31/2007#), cnt + 1, 1) _
, DateSerial(Year(#10/31/2007#) + 1, cnt + 1, 1) _
, DateSerial(Year(#10/31/2007#) - 1, cnt + 1, 1) _
, DateSerial(Year(#10/31/2007#), cnt + 1, 1)
Next cnt
End Sub

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
strSQL = "SELECT Sum(tblNonCalcSpreads.Depreciation) AS Depr " & _
"FROM (tblNonCalcSpreads INNER JOIN tblClients " & _
"ON tblNonCalcSpreads.ClientNumber = tblClients.ClientNumber " & _
"WHERE tblNonCalcSpreads.DateOfData BETWEEN " & _
"IIf(Month(#" & dtDate & "#)>[tblClients].[FYE],DateSerial(Year(#" & dtDate & "#),[tblClients].[FYE]+1,1), " & _
"DateSerial(Year(#" & dtDate & "#)-1,[tblClients].[FYE]+1,1)) AND " & _
"IIf(Month(#" & dtDate & "#)>[tblClients].[FYE],DateSerial(Year(#" & dtDate & "#)+1,[tblClients].[FYE]+1,0), " & _
"DateSerial(Year(#" & dtDate & "#),[tblClients].[FYE]+1,0))-1;"
I have aliased the tables and removed the [ ] as they are not required in this code to reduce the legnth of the SQL String
You coulkd eliminate the IIF completely as Month(#" & dtDate & "#) > C.FYE will return either 0 or -1 but that makes it harder to read.
Cheers, Andrew
strSQL = "SELECT Sum(S.Depreciation) AS Depr " & vbCrLf & _
"FROM (tblNonCalcSpreads S INNER JOIN tblClients C" & vbCrLf & _
"ON S.ClientNumber = C.ClientNumber " & vbCrLf & _
"WHERE S.DateOfData " & vbCrLf & _
"BETWEEN DateSerial(Year(#" & dtDate & "#)-IIF(Month(#" & dtDate & "#) > C.FYE,0,1),C.FYE+1,1)" & vbCrLf & _
"AND DateSerial(Year(#" & dtDate & "#)+IIF(Month(#" & dtDate & "#) > C.FYE,1,0),C.FYE+1,1)" & vbCrLf






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
I tried that and I receive a syntax error.
I receive a syntax error in the JOIN operation.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Cheers, Andrew






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Microsoft Access cannot open this file.
The file is located outside your intranet or on an untrusted site. Microsoft Access will not open the file due to potential security problems.
To open the file, copy it to your machineor an accessible network location.
This is after saving it to My Documents or on my desktop.
Cheers, Andrew
GRayL you are correct the code is returning all the data for the FY the date entered is in. All the data has FYE of 12 and when you enter the date 31/03/07 you do indeed get 52 as this is the total for 2007, it is also the total for all the data and this I beleive is causing confusion and this is a case where more data will show the query working correctly. There are several records for 2008 that are NULL, try putting data into these and see the results of the query.
The original question talks about Current FY, if you pass in todays date it will work for the current FY, the problem is there is no data to return for the current FY and the answer returned is NULL. This will be the Year To Date figure if you do not post data ahead of today.
Finally if you do post data with a date in the future then the code below, as GRayL stated earlier, will work for you.
Cheers, Andrew
strSQL = "SELECT Sum(S.Depreciation) AS Depr " & vbCrLf & _
"FROM tblNonCalcSpreads S INNER JOIN tblClients C" & vbCrLf & _
"ON S.ClientNumber = C.ClientNumber " & vbCrLf & _
"WHERE S.DateOfData " & vbCrLf & _
"BETWEEN DateSerial(Year(#" & dtDate & "#)-IIF(Month(#" & dtDate & "#) > C.FYE,0,1),C.FYE+1,1)" & vbCrLf & _
"AND Date()" & vbCrLf

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Chers, Andrew






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Microsoft Access
--
Questions
--
Followers
Top Experts
Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.