How Can I Perform Date Comparisons in Access 2013 Query Criteria? - ms-access

I have a date field in my table, and I'm writing a query in Access 2013 to select all items where the date is between 7-days-ago and 30-days-in-the-future.
Currently, I've added the following as "criteria" under the date field:
>=Today()-7 And <=Today()+30
But I get the following error when I try to save the query:
I've tried using DateDiff (as I have in other scenarios) but it tells me that I'm not allowed to use that type of expression as criteria.
EDIT: This is an Access 2013 custom web app for SharePoint 2013, and all the available functions and syntaxes appear to be different from those available in a desktop database file.

You might be confusing with the Excel function named TODAY(). In Access it is called Date().
You can also use Between..And.
Between Date()-7 And Date()+30
Added In response to advice about using SharePoint:
I don't use SharePoint, but might guess that you need to specify the field explicitly:
fieldName >= Today()-7 And fieldName <= Today()+30
you might use brackets to make the statement clearer:
(fieldName >= Today()-7) And (fieldName <= Today()+30)

Related

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 :)

setting value of parameter for query using sql in microsoft access

I have a query in access as follow:
select id, sum(amount) as totamt from expense group by id having year=[Year];
When I run this I get a prompt for year, I type say 2015 then click enter and the data appears for 2015.
Is it possible to create a new query (say exp2015) that set the year parameter to 2015 before calling the original query so that it would return data for 2015 without prompting the user.
I'm afraid it's impossible. Each query instance has own independent set of parameters in memory. Try to redesign base query - use form field or function instead of parameter. You can also set parameter value using VBA.

MS Access Convert text to shortdate

Hi all i have a question,
I have a field in the table whereby its a text field and i want to convert it to date field. Though all the values within are in date format however its not convenient to change because of some circumstances.
Thus i need to change the text field to date field so as to do a comparison or validation.
Im using MS access SQL for this project so please help me
I have tried
TRANSFORM Count(Registrants.[Field1]) AS CountOfField1
SELECT Registrants.[Country] , Count(Registrants.[Field1]) AS [Total Of Field1]
FROM Registrants
WHERE Cast(Registrants.Field1 As Date) Between #15/6/2014# AND # 30/8/2014#
GROUP BY Registrants.[Country]
PIVOT Format([Field1]);
Cast does not exist in MS Access sql.
Use DateValue instead :
WHERE DateValue(Registrants.Field1) Between #15/6/2014# AND #30/8/2014#
See here for sample usage and syntax.
Also I noted a space in your 2nd date, I assume that's a typo.

Microsoft Access 2010 Web Databases Convert Date to String

I am trying to build a calculated field for a web database in Microsoft Access 2010.
The field should show the day in a three character format as follows:
Mon
Tue
Wed
from an existing date column in the table.
Obviously the Format function is not compatible with the web. And the following function FormatDateTime does not have the option to convert to a string day, just a long and short date and time.
Any ideas on how this can be accomplished?
If this is not possible my only option seems to be to create a client object.
I have tried adding the function into a query instead but this is also incompatible with a web database.
WeekdayName should suit. Be careful with FirstDayofWeek:
SELECT WeekdayName(Weekday([ADate],1),-1,1) FROM Table
Syntax:
Weekday(Date,[FirstDayOfWeek])
WeekdayName(Weekday as Long,[Abbreviate as Boolean],[FirstDayOfWeek])
1 = Sunday in both of the functions.

MS Access 2007: date query

I need help with date queries in MS Access 2007.
How do I show all data between date:01/06/2010 time:10:51 and date:13/07/2010 time:22:30?
If you are using the query design window you have a lot more latitude than if you are working in VBA. In the query design window you can enter a date and time on the criteria line in the format for your locale, when viewed in SQL view, you might see:
SELECT tbl.CrDate
FROM tbl
WHERE tbl.CrDate Between #2/5/2006 14:7:0# And #11/18/2006 17:28:15#
However, it is generally best to enter dates in year/month/day or year-month-day format, even though Access may change it to your locale format. In VBA it is a different story, Access needs month,day,year order or year,month,day. Once again, year,month,day is better.
As regards your problem, if you have separated the date and time fields, it would be best to reunite them for the query, you can use + :
DateField + TimeField Between #01/06/2010 10:51# And #13/07/2010 22:30#
I have not used MS Access for years, so this is only from memory: Access uses # instead of ' for date values. And you need to use ISO format:
WHERE datecolumn >= #2010-06-01 10:51# AND datecolumn <= #2010-07-13 22:30#