Hello. Plog was kind enough to get me started by responding to this question over in queries:
I am trying to return a value in an expression (call it FundedPeriod): CurrentWeek, CurrentMonth, PreviousMonth, based on a date value in field [funded_date].
Here are the criteria I am using:
Current Week: DatePart("ww", [Funded_Date]) = DatePart("ww", Date()) and Year([Funded_Date]) = Year(Date())
Current Month: Year([Funded_Date]) = Year(Now()) And Month([Funded_Date]) = Month(Now())
Previous Month: Year([Funded_Date])* 12 + DatePart("m", [Funded_Date]) = Year(Date())* 12 + DatePart("m", Date()) - 1
plog got me started with this:
Based on his example, I expanded upon that and came up with this:
Am I in the ballpark w/ this in order to return:
CurrentWeek or CurrentMonth or PreviousMonth
so in the query expression I think I would type FundedPeriodName: FundedPeriod([funded_date])
Thanks for input.
I am trying to return a value in an expression (call it FundedPeriod): CurrentWeek, CurrentMonth, PreviousMonth, based on a date value in field [funded_date].
Here are the criteria I am using:
Current Week: DatePart("ww", [Funded_Date]) = DatePart("ww", Date()) and Year([Funded_Date]) = Year(Date())
Current Month: Year([Funded_Date]) = Year(Now()) And Month([Funded_Date]) = Month(Now())
Previous Month: Year([Funded_Date])* 12 + DatePart("m", [Funded_Date]) = Year(Date())* 12 + DatePart("m", Date()) - 1
plog got me started with this:
It's time to move your logic to a function inside a Module. Instead of trying to jam all those comparisons into one line, create a custom function, pass it [Funded_Date] and use as many lines as you need to determine what the result should be. I'll start you out:
Paste the above into a module, add more of your logic and then use it in a query by using this code:Code:Function IsCurrentWeek(FD) ' returns True or false based on if FundedDate (FD) is in current week Dim ret As Boolean ret=true ' default return value is true, if fails any of below tests, its set to false If (DatePart("ww", FD) <> DatePart("ww", Date()) ret=false IsCurrentWeek = ret End Function
CurrentWeek: IsCurrentWeek([Funded_Date])
Based on his example, I expanded upon that and came up with this:
Code:
Function FundedPeriod(FD)
' returns CurrentWeek or CurrentMonth or PreviousMonth or None based on FundedDate (FD) criteria
Dim ret As Boolean
ret=CurrentWeek
' default return value is true, if fails any of below tests, its set to false
If (DatePart("ww", FD) = DatePart("ww", Date()) ret=CurrentWeek
ElseIf Year([Funded_Date]) = Year(Now()) And Month([Funded_Date]) = Month(Now()) ret=CurrentMonth
ElseIf Year([Funded_Date])* 12 + DatePart("m", [Funded_Date]) = Year(Date())* 12 + DatePart("m", Date()) - 1 ret=PreviousMonth
FundedPeriod = ret
End Function
CurrentWeek or CurrentMonth or PreviousMonth
so in the query expression I think I would type FundedPeriodName: FundedPeriod([funded_date])
Thanks for input.