Date calculations - how to use today's date if date not specified - ms-access

I have a question regarding Microsoft Access 2010. I'm working on an inherited complaints database and I've been asked to produce a report outlining how long complaints have/ or were active.
Using the following I'm able to calculate the difference between when a complaint was opened and when it was closed and show the number of days active.
DaysActive: DateDiff("d",[COMPLAINTS]![DateRcvd],[COMPLAINTS]![DateClosed])
My issue is when a complaint hasn't been closed I don't get a value returned. Is there a way to modify the expression so that if the DateClosed is empty it will use the current date instead?

Assuming this is a query expression, the db engine supports the Date() function which returns today's date. So you can use an IIf expression to give you Date() when DateClosed is Null, or otherwise DateClosed
DateDiff("d", COMPLAINTS.DateRcvd, IIf(COMPLAINTS.DateClosed Is Null, Date(), COMPLAINTS.DateClosed))
If the query will always be run from within an Access session, you can use Nz instead of IIf ...
DateDiff("d", COMPLAINTS.DateRcvd, Nz(COMPLAINTS.DateClosed, Date()))
Note that Nz is a VBA function and IIf is supported directly by the db engine, so IIf should theoretically be faster . But the difference may not be perceptible in your context.

Try using this.
DaysActive: DateDiff("d",Nz(COMPLAINTS.DateRcvd, Date()),[COMPLAINTS]![DateClosed])
Nz will check if the date is available or not, if not , it will replace today's date for that, using DATE() function.

Related

SSRS Time expression filter not working with Parameter boolean value

Background:
Trying to achieve a filter in dataset, if the user selects Day shift from the parameter, any data entry between 06:00:00 and 18:00:00 is filter in the report. Furthermore, any data entry between 18:00:00 and 06:00:00 is reflect in the report for Night shift
Process:
I have got a parameter with 2 Boolean values "True" and "False". I have labeled those values as "Day shift" and "Night shift" in the available value Parameter window option. Default value is true.
Converting datetime to time, in the dataset query
SELECT
CONVERT(time, SWITCHOFFSET(CONVERT(datetimeoffset, LastModified),DATENAME(TzOffset, SYSDATETIMEOFFSET()))) as ShiftTime
FROM table abc
Filter expression:=Fields!ShiftTime.Value
Operator: in
Value: =IIf( ( TimeValue(Fields!ShiftTime.Value) >= "06:00:00" And TimeValue(Fields!ShiftTime.Value) <= "18:00:00" ) , Parameters!ShiftType.Value(0), Parameters!ShiftType.Value(1) )
Problem: [BC30516] Overload resolution failed because no accessible
'IIf' accepts this number of arguments
Not sure which part I am going wrong with, I am thinking it is the datatype but unsure.
Solution which I built on
I'm not sure what report designer you are using, but using the Report Designer extension for Visual Studio gives me very different errors.
And it gives errors both in the output window, and the expression window when building it.
Firstly to debug this, add it as a column to your report before you try and implement it as a filter.
Then check the relevant documentation for the function. It turns out the TimeValue function takes a string, not a DateTime, and returns a datetime.
Then, you can't access the available parameter values from the parameter, you can only access the selected parameter values. So instead of Parameters!ShiftType.Value(0) (which has a red line under it) you should just have true (or false depending on which way around your logic is) i.e. the value of the parameter.
Finally, I don't know what implicit conversion SSRS does from strings to a time, so I would do an explicit time compare instead of a string compare. Given we know that TimeValue returns a DateTime with the minimum date value and the time component we can compute the required limites for day/night shift.
The following is what worked for me:
=IIf(TimeValue(Fields!ShiftTime.Value.ToString()) >= DateTime.MinValue.AddHours(6) And TimeValue(Fields!ShiftTime.Value.ToString()) <= DateTime.MinValue.AddHours(18), true, false)
And actually re-visiting it, thats way too convoluted, you are already returning a time value which we can compare directly as
=IIf(Fields!ShiftTime.Value >= new TimeSpan(6,0,0) And Fields!ShiftTime.Value <= new TimeSpan(18,0,0), true, false)
And then your filter should be:
Filter expression: {as shown above}
Operator: =
Value: =Parameters!ShiftType.Value
Note: when you say "between" I don't know whether the intention is to include both 6am and 6pm within the day shift window, but I haven't modified your logic in that regard.

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.

Can't get the Today() function to work properly in MS Access 2016 Custom Web App

I'm trying to setup a query that will show me all of the records in a particular table where the listed expiry date is either in the past or upcoming in the next 6 months (approximately).
At the moment, I have the "Expiry" field added to my query and the 'Criteria' as .
When I try to save the query, I get the following message:
Access can't evaluate an expression or convert data because data types aren't compatible with each other.
TECHNICAL DETAILS
Correlation ID: ae68949d-3041-3000-0984-71635f8fd670
Date and Time: 7/28/2016 6:54:34 PM
I've tried searching the web for a solution, but most websites refer to the Date() function that doesn't seem to be available in the Access 2016 Custom Web App. When I take out the "+180", it works fine but obviously doesn't give me what I need.
Any help would be appreciated.
=============================
UPDATE:
Some users have asked for my SQL and Table Design details. I don't seem to have any way of accessing the SQL View (the option doesn't appear), but here's a copy of my table view:
Access Query Table Design
In the table, 'Active' is a Yes/No field and 'Expiry' is Date/Time.
Try
< DateAdd(Day, 180, Today())
as criteria.
According to https://msdn.microsoft.com/en-us/library/office/jj249452.aspx this should work in a custom web app.
Error says that you have two different date types and they can't be compared. so, as the Today() returns only the Date with 12:00 pm, I can guess that your other "Expiry" Field is a datetime type. So, you can do either: use Format function to convert datetime to date like this
Format([2/22/2012 12:00 PM],"dd/mm/yyyy")
or use Now() function which returns datetime,
or Share your Code :)

Access VBA filtering SQL by short date on a long date/time field

I am having an issue getting criteria to work. There is a related question on StackO here, but I have tried what's in it and it's still not working, so I'm asking my specific question. Apologies if that's not technically kosher at StackO.
Can't filter MS access datetime field using short date
I have a query that I need to have WHERE criteria based on an associate ID and the current day’s date. But the field that has the current day’s date needs to be a long date and time field, so criteria for short date doesn’t work. I have made this query in query design mode and it does exactly what I want:
SELECT tbl_Data.[#], AssocID, tsUpdated FROM tbl_Data WHERE AssocID = 4441 AND DateValue([tsUpdated])=Date()));
But when I do the equivalent in VBA, which I need to have this happen in, it does not work. Please note, I have used Date in this vba version because according to this website, Date will return current date:
Set FinishReport = CurrentDb.CreateQueryDef("qry_SessionReport", "SELECT tbl_Data.[#], AssocID, tsUpdated FROM tbl_Data WHERE AssocID = 4441 AND DateValue([tsUpdated])=Date”)
I have tried all kinds of syntax, and I have tried the answers from that StackO URL above, but nothing has worked. Any thoughts?
Thanks!
For the querydef, I noticed you don't have the closing parenthesis for the Date() function in the SQL text. In the VBA editor, the function is listed as Date with no parenthesis but if you tried to use the Date() function in a query without the parenthesis Access would throw an error. Hope that helps!

Comparing todays date against first day of the week

I am trying to set a default parameter value in SSRS report. I want to test the current date to see if it equals the first day of the week (in my case Monday). If it is the first day of week, then I want the default value to be current date minus 2 days, if it is not the first day of the week then I want the default value to be current date minus 1 day.
I seem to have a syntax problem but it doesn't tell me where. My parameters are StartDate and EndDate.
this is what I've tried:
=iif(weekday(Today(),FirstDayOfWeek.Monday)==1,DateAdd("d",-2,today(),DateAdd("d",-1,today())
this is the generic error I get:
The value expression for the report parameter 'StartDate' contains eror:[BC30201] Expression expected.
Where am I going wrong?
You are trying to use Sql syntax in a SSRS VBA expression. SSRS VBA allows very similar expressions for date manipulation to Sql, the main difference being the use of the DateInterval enum.
So your expression needs to use VBA syntax:
=IIF(Weekday(Today, FirstDayOfWeek.Monday) = 1, DateAdd(DateInterval.Day, -2, Today), DateAdd(DateInterval.Day, , -1, Today))
It appears that you are missing a closing parentheses after the first logical part of the if statement and another to close the statement.
=iif(weekday(Today(),FirstDayOfWeek.Monday)==1,DateAdd("d",-2,today()),DateAdd("d",-1,today()))