How to calculate the difference between days in a table field - ms-access

Ok this should be a relatively easy thing to do, yet I'm at the head desk stage trying to figure out the insanity here.
I have a table called tblPersonnel. I'm tracking two document expiration dates in date/time fields called CED and PPED. When I run a query against tblPersonnel I need it to look at PPED, determine if that document is expired and if so use CED instead. I have a few fields in the query that need to use this concept to determine what the output value is, but I am hitting a wall here trying to get the query to spit out the correct value. Here's what I'm using for one of the fields - Document Expiration Date: IIf([PPED]-Now()<0,[CED],[PPED]). What's happening is that the expression is constantly popping as false, so PPED is getting used regardless if it's an expired date or not. Does anyone have any ideas as to what I'm doing wrong here?
I've also tried to set this up as its own field in tblPersonnel, but that's even more aggravating. If I try to set the field to just a text field - IIf([PPED]-Now()<0,"Yes","No"), the formula will accept the use of Now(), but it doesn't like the reference to the other fields in the table. If I set it as a calcuated column, I can reference the other fields but it doesn't like Now(). I'm at a loss here.

If PPED is less than Date(), it is expired. Don't need to subtract. Assuming CED and PPED are just date parts, no time, consider:
IIf([PPED] < Date(), [CED], [PPED])
If PPED could be null:
IIf(Nz([PPED],0) < Date(), [CED], [PPED])

Ok finally fixed it here. I had another issue in that I wasn't accounting for how Access would handle a Null or blank value in PPED. The functioning formula is Document Expiration Date: IIf(Len([PPED])>0,IIf([PPED]<Date(),[CED],[PPED]),[CED]) Thanks to June7 for helping me simplify the expression, as I was using DateDiff('d',[PPED],Date())<0 but their answer is just so much cleaner and quicker to type.

Related

Why does this expression work in one spot but not another when using the same data?

I have 2 reports here that are machine schedules (Wrap & Moulder). They were working great, until I tried to add a checkbox to say whether or not the previous step had been marked completed. It worked initially on Wrap Schedule but when I did the exact same thing on Moulder Schedule it is coming out as data type mismatches. In the checkbox I have put the expression:
=IsDate([Previous_Date_Ran])
It works great on Wrap Schedule, but as soon as a Date is entered in the [Previous_Date_Ran] field I am getting data type mismatches. I have narrowed it down to whenever a date is put into that field, which is odd because when it is Null there is no issue. I have made sure already that my other form that supplies this [Previous_Date_Ran] field is inserting =Date() and not =Now() or =Time(). Below will be screenshots of it working on Wrap but erroring on Moulder.
[
Thank you if you have suffered through with me so far, any help would be much appreciated!
For anyone still struggling here is the steps I took to fix this problem:
Firstly, I have the field Date_Ran as a Date/Time, formatted to short date. Another form has an on-dbl-click event where it inserts today's date in the field. Turns out when you use =Date() in VBA is assigns it a default Variant type. For whatever reason I believe this was what caused the error. The second I switched to using =Date$() it inserts today's date as a string and the error has stopped. Good luck!

How do I set a date field's default value to the day's date?

I'm building an HTML/ColdFusion page and have a field type of "date". How do I set the value for that field to the day's date that the user is signed on - i.e. the "now" date? It sounds like it should be easy but I can't find a way to make it work. I'm fairly new to coding so please keep answers simple.
Actually I found the answer using the ColdFusion date format function such as TodaysDate=DateFormat(Now(),"YYYY-MM-DD") and then setting the value to TodaysDate.

Error in Expression used for Control Source in an Access Form

I actually have two questions.
I'm a beginner user of Access, still trying to get a good understanding of the software. I'm trying to create a database for a library (School project) with a borrowing out system. I have two fields in a table called DueDate and DateHired. The DueDate functions on the expression =Now()+28 and the DateHired function on the expression =Now(). Basically making the due date 4 weeks ahead of when the book was hired. My first question is quite simple; if I were to input a record today, would the two DueDate/DateHired fields remain the same date and time by tomorrow? Or would they update to the Now() of tomorrow?
My second question is something regarding an expression. I'm trying to make an Overdue checkbox. The premise is that if Now()>DateDue then the Checkbox will be 'Yes'. My current code in the ControlSource part is this:
=CBool([DateDue]
However, the checkbox simply displays '#Error' and not Yes/No. I'm also concerned that if the answer to the first question was '=Now() stays the same after the record is added and doesn't update' that would also mean the Overdue function would not really work unless you were inputting the record after the due date. Which would make no sense. Any suggestions?
Cheers.
This is relation to your second question. You can ask a separate question for the first part.
=CBool([DateDue]
What you are trying to do here, is convert a Date data type to a Boolean (you're missing the closing parentheses by the way) which is impossible.
What you should be doing is check if the due date is less than today and return the appropriate True/False value.
IIf([DueDate] < Date(), True, False)
Which means:
IIf("Is due date in the past?", "Yes, it's overdue", "No, it's not overdue")
You can read more about the IIf function here.
Indeed as a beginner, make it a habit to use the date functions. Later you can turn to "smarter" methods which, by the way, often aren't that smart.
1.
If you store a date, it remains unchanged in the table. And don't use Now unless you specifically need the hours too:
DateDue = DateAdd("d", 28, DateHired)
or in a query - using the designer:
DateDue: DateAdd("d",28,[DateHired])
or as a ControlSource for a textbox on your form:
=DateAdd("d",28,[DateHired])
2.
You can use DateDiff for this:
Due = DateDiff("d", DateHired, Date) > 28
or in a query - using the designer:
Due: DateDiff("d",[DateHired],Date()) > 28
or as a ControlSource for a textbox on your form:
=DateDiff("d",[DateHired],Date()) > 28
and set the Format property of the textbox to, say, Yes/No or something else meaningful for your users.

SSRS Iif always true when false part contains expression

I've got a report outputting to an Excel file, with some fields in a database being empty.
I'm trying to filter these null values out using the following Iif expression in a cell:
=Iif(
IsNothing(Fields!START_DATETIME.Value),
0,
Fields!START_DATETIME.Value
)
This doesn't work, I get "#VALUE" in the cells where the data was null, and the time value of the cell where there was data.
If I use the same expression without an expression as the third parameter:
=Iif(
IsNothing(Fields!START_DATETIME.Value),
0,
1
)
I get 1s where there is a date in the table and 0s where there is nothing.
I'm sorry if this is a duplicate, I couldn't find anything that worked for me...
Edit:
I'm using VisualStudio Professional 2013
Update
What I've got for each entry in the database is: a datetime for authorization time, another for start time and another for stopped time. There is always an entry for authorization time, but not always for start and stopped.
I've split the date and time out from the various datetimes into columns like so:
Authorized date
Authorized time
Start time
Stop time
The idea is that if no start and stop time are present, just use the authorized time
The following expressions are put in the Start time column. Sorry, I don't have enough rep to post images or more than 2 links. I've put program output in this album:
http://imgur.com/a/zJSAY
Doing:
=Iif(Fields!START_DATETIME.Value is Nothing,
DateAdd(DateInterval.Day,693594,TimeValue(Fields!AUTH_DATETIME.Value)),
DateAdd(DateInterval.Day,693594,TimeValue(Fields!AUTH_DATETIME.Value))
)
Outputs the authorized time, so that works fine.
However, doing:
=Iif(Fields!START_DATETIME.Value is Nothing,
DateAdd(DateInterval.Day,693594,TimeValue(Fields!AUTH_DATETIME.Value)),
DateAdd(DateInterval.Day,693594,TimeValue(Fields!START_DATETIME.Value))
)
Outputs the start time where there is an entry, and #VALUE! (null or some such) when there isn't anything.
And doing:
=Iif(Fields!START_DATETIME.Value is Nothing,
DateAdd(DateInterval.Day,693594,TimeValue(Fields!START_DATETIME.Value)),
DateAdd(DateInterval.Day,693594,TimeValue(Fields!AUTH_DATETIME.Value))
)
Outputs the authorized time where there is an entry in start time, and #VALUE! when there isn't anything.
I can't figure this out. I understand (1) and (3), but what I need is for (2) to display the correct start time while there is one, and the authorized time when there isn't.
Update 2
It turns out that Iif() evaluates each conditions even if it is not selected, and any error encountered is passed on regardless. See comments to Why isn't my iif statement evaluating true when the condition is correct?
To fix this I did:
=DateAdd(
DateInterval.Day,
693594,
TimeValue(
Iif(
Fields!START_DATETIME.Value is Nothing,
Fields!AUTH_DATETIME.Value,
Fields!START_DATETIME.Value
)
)
)
This seems to work for me, and should have been the common sense way to do this in the first place...
Thanks again for your help guys.
Excel will guess the type of a column based on the first few cells. If there's typical DATETIME values in those, it'll guess the column is a date column, and it will error with "#VALUE! on values that don't match the type, such as "0".
You should ask yourself what you want to display in Excel for NULL values:
If you just want an empty cell, don't use an expression at all. Just Fields!START_DATETIME.Value will render a NULL value as an empty cell.
If you want a "baseline" datetime, make sure to use that instead of "0". Easiest is probably in your query with something like ISNULL(START_DATETIME, GETDATE()).
If you really want a "0", make sure it appears in one of the first few cells by ordering. Excel will see the "0" and not set the column type to date/time.

SSRS: Summing TimeSpan values in a report

I have a report and a datasource where one of the columns are of type TimeSpan. The TimeSpan value appears to display correctly in the report when I use Fields!TheTime.Value, no problem there.
07:02:00
05:41:00
But I would like to do a Sum on those values to get the total time of a group. In C# and such I can of course do a TimeSpan + another TimeSpan, so I know they can be added. I tried
=Sum(Fields!TheTime.Value)
But it ends up printing out as a long number of some sort. For example for the outputted times above, I would get 457800000000 as the sum. And what is that even supposed to be?
Anyways, how can I sum timespan values in a report? For the above timespans I would like to end up with 12:43:00 as the sum. Unless my head failed me at math once again... but you get the idea :p
sigh The solution annoyingly simple... Why couldn't I just have tried that in the first place? Oh well... maybe because I didn't realise I had access to TimeSpan class... maybe because I had thought myself blind... But anyways, here it is:
=TimeSpan.FromTicks(Sum(Fields!TheTime.Value))
D'oh!
#Svish - I deleted my previous post because I had a fit uncertainty about my answer but I concur with #pfunk.
I finally got SSRS back up and had a play around and it certainly looks like your big number is the number of ticks so it looks like a bit of formatting of the result will work for you.
Interestingly enough my previous convoluted answer was a workaround for summing DateTime values (using SQL Server DATETIME datatype in my query) which you cannot do in SSRS (and SQL) because you cant sum a DATETIME. I'll include it here again for future reference but I think was on a bit of a tangent earlier :)
The below code converts a DateTime field into a double, sums the result and then converts it back to DateTime and formats it for hh:mm:ss
=Date.FromOADate(Sum(Fields!TheTime.Value.ToOADate())).ToString("hh:mm:ss")
What is probably happening is that when you display Fields!TheTime.Value, SSRS is smart enough to know to display that as a DateTime type field
when you add the sum in there it thinks it is a numeric type field and displays it as such (ie, it is summing the number of "ticks" in each timespan field)
try specifically formatting the summed value as a datetime in the field properties and it will probably show correctly