How to stop date when someone dies - date

I have an access database for my dairy farm. I have one field named DateBorn, a module function fAge, and an unbound field named AgeNow. For this I have the expression:
=IIf(IsNull([DateBorn]),"",fAge([DateBorn]),Date()))
With this expression, whenever I type in a value for DateBorn, it calculates age for me in years, months, and days. It has worked fine thus far.
Now I want to add something to it; another field named DateDied. I want an expression that whenever I put DateDied, it should stop calculating age for that particular record.

I'm not sure if you made a mistake in your sample regarding calling the function fAge().
I expect it needs two date parameters.
That expression always calculates the age, but for people who died it uses DateDied instead of Date():
=IIf(IsNull([DateBorn]),"",fAge([DateBorn],NZ([DateDied],Date())))
If in case of dead people there shouldn't be any calculated age use this:
=IIf(IsNull([DateBorn]) Or Not IsNull([DateDied]),"",fAge([DateBorn],Date()))

Related

MS Access use DATE() in a calculated field

I am using Microsoft Access 2016. I am trying to find out how many years exist from the current year until a future year. I have a column that is end_date. I am trying to create a calculated field that is essentially YEAR(end_date) - YEAR(current_year). I tried to use YEAR(DATE()) but DATE() is not allowed to be used in a calculated field apparently.
Is there no way to do a calculation like this?
Nope. Calculated fields are cached and static, so are NEVER allowed to contain ANY information that will change over time, due to system settings, or anything else that is not directly entered in that row.
However, you should not be using calculated fields anyway. See http://allenbrowne.com/casu-14.html, among many posts advocating for not using calculated fields.
Instead, use queries to do calculations. That way, you won't have any trouble using the current date, and won't have to deal with the possible errors and portability issues calculated fields come with.
I changed my thinking to calculate this in a form. It does not seem good practice to have a field in a DB that changes everyday.
In a form, you can use this expression as controlsource for a textbox:
=DateDiff("yyyy",Date(),[EndDate])
However, that return the difference in calendar years. To find the count of full years, use a function like AgeSimple and this expression:
=AgeSimple([EndDate])

Is there a way to have number type parameters blank?

I'm creating a demographics report where users can choose what demographics they want to include on the report. One of the options is age range where the user can type in their own figures. Everything works except when the age range parameters are not used. The others can be blank as they are string types but the age range parameters are number types because I need to use them to do counting. Is there a way to use number type parameters but allow them to be blank if necessary?
I tried changing the type to string and then running a ToNumber formula to use the field for my needs but I get the same error. I also tried suppressing any fields that use number parameters but trying to say suppress is Not HasValue but that didn't work either.

Deterministic function for getting today's date

I am trying to create an indexed view using the following code (so that I can publish it to replication it as a table):
CREATE VIEW lc.vw_dates
WITH SCHEMABINDING
AS
SELECT DATEADD(day, DATEDIFF(day, 0, GETDATE()), number) AS SettingDate
FROM lc.numbers
WHERE number<8
GO
CREATE UNIQUE CLUSTERED INDEX
idx_LCDates ON lc.vw_dates(SettingDate)
lc.numbers is simply a table with 1 column (number) which is incremented by row 1-100.
However, I keep getting the error:
Column 'SettingDate' in view 'lc.vw_dates' cannot be used in an index or statistics or as a partition key because it is non-deterministic.
I realize that GETDATE() is non-deterministic. But, is there a way to make this work?
I am using MS SQL 2012.
Edit: The hope was to be able to Convert GetDate() to make it deterministic (it seems like it should be when stripping off the time). If nobody knows of a method to do this, I will close this question and mark the suggestion to create a calendar table as correct.
The definition of a deterministic function (from MSDN) is:
Deterministic functions always return the same result any time they are called with a specific set of input values and given the same state of the database. Nondeterministic functions may return different results each time they are called with a specific set of input values even if the database state that they access remains the same.
Note that this definition does not involve any particular span of time over which the result must remain the same. It must be the same result always, for a given input.
Any function you can imagine that always returns the date at the point the function is called, will by definition, return a different result if you run it one day and then again the next day (regardless of the state of the database).
Therefore, it is impossible for a function that returns the current date to be deterministic.
The only possible interpretation of this question that could enable a deterministic function, is if you were happy to pass as input to the function some information about what day it is.
Something like:
select fn_myDeterministicGetDate('2015-11-25')
But I think that would defeat the point as far as you're concerned.

MS Access calculated fields changing to "#Error"

I currently have a form that runs off of a query and everything works swimmingly, or at least it appears. I have three fields in question: an ID field, a Client Name field, and an Age field. The age field is actually the client's age at the signing of the quote, so it is a calculated field using the date of the quote and the client's DOB.
The age calculation works perfectly upon first look. The reason the ID and name fields are in question are because when I double click them it opens a form, which yet again works fine. The problem is when I exit those forms and come back to the original form.
The age field of whatever record I was on changes to "#Error" instead of what it was displaying. If i click on a different record in the form then that age field changes to "#Error" as well. This seems to happen every time and I have no clue why.
Has anyone solved an issue similar to this or have an idea on how to solve it?
Thanks in advance!
Update: I changed it to open a report rather than a Form and the issue is still there.
The calculation in question: Int(([effectiveDate]-[dateOfBirth])/365.25)
Try calculating age this way:
DateDiff("yyyy", [dateOfBirth], [effectiveDate]) + ([effectiveDate] < DateSerial(Year([effectiveDate]), Month([dateOfBirth]), Day([dateOfBirth])))
Using this method takes the difference in years between your date of birth and the effective date, taking into account that the person's birthday may not have passed yet by that date.
Basically the first part gets the years and the second part either adds 0 for false or adds -1 for true if the effective date is LESS than the birthday for the effective year (meaning it hadn't passed yet)

Get the last date of non-empty value using MDX

Do you know how to get the last non-empty value using mdx query.. then after that.. i want to count how many null values are next to it up to the last date. My main purpose for this is to count how many days a customer has no transaction..
i have to make a report in ssrs (using adventure works cube) that counts how many days a customer has no transaction..
thanks..
Use this article to obtain set of non empty dates.
To obtain count you can:
Use member properties (day of year or something more suitable in
this case) and just subtract one value from another.
You can find
last date with non empty value, previous one using LAG(1), include
them to the next construction {prev.date:last.date} and use COUNT
for set
Recently I follow this article, It really helps me.
The most important part is to understand scope usage.
Even If you don't have an Enterprise Edition, you can use it.