MS Access 2007: date query - ms-access

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#

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

Classic ASP, DATE difference between IIS6 and IIS8

I am helping to migrate a classic asp website (front end) and ms access database (back end) from a Windows 2003 IIS6 server to a Windows 2012 IIS 8.5 server. I am having a problem with this query in particular;
sqlquery1 = "Select RentalNum, CarID, RentalStatus, StartDate, EndDate from table where CDate(EndDate) <= CDate('"&Date()&"') order by CDate(EndDate) desc"
On the existing system all is ok. On the new system the returned results are not <= "todays date". The results show some dates before today and some after. The database date fields are just "text" (I didn't set it up) and whilst my initial thoughts were to change the schema to proper dates, I would like to understand the problem, particularly as there are many of parts of the system using similar queries using date() and CDate. Are there underlying dates differences between IIS servers? I have looked at browser locality and all is ok there.
Any pointers?
Examine this expression used in your query:
CDate('"&Date()&"')
The Date() function returns the system date as a Date/Time value. But then that expression adds quotes before and after the Date/Time value, which transforms it to a string value. And then CDate() takes that string and transforms it back to a Date/Time value again.
Hopefully that description convinces you those manipulations are at best wasted effort. However if the two servers have different date format settings, the dates resulting from that full CDate() expression could be different.
I'm not positive that is the source of your problem, but you can easily eliminate the possibility. The Access db engine supports the Date() function, so you can use it directly without first transforming it to a string and then back into a Date/Time value.
sqlquery1 = "Select RentalNum, CarID, RentalStatus, StartDate, EndDate " & _
"from [table] where CDate(EndDate) <= Date() order by CDate(EndDate) desc"
If that change does not solve the problem, next examine those EndDate text values and make sure they're interpreted as the date you expect:
SELECT EndDate, CDate(EndDate) AS EndDate_as_date
FROM [table];
You mentioned your "initial thoughts were to change the schema to proper dates". I think that is the best way to go because developement based on dates as Date/Time is saner than with dates as strings.

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.

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

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)

Last Active List - VB.NET

I've got a client application that's going to update a database every five minutes with the current time, and then I want to output this time as a last active table in a seperate VB application.
I know about mysql time, but I don't quite understand how I can use it to display when a client was last active.
I've looked around and found some stuff about mysql times but I don't fully understand it.
Any help would be great, I'm going to place the results in a ListView with 'Client Name' and 'Last Active' if this helps, and I already know how to connect to my database and retrieve information.
Thank you.
I'd recommend using a DATETIME for storage. The TIME data type is limited to a single "time of day" or a timespan. True, you're looking for the time of day, but to calculate the "Last Active" time you need the date attached. Consider these "Last Active" values (using a 24-hour clock):
3/26/2013 at 17:00:00 <-- this has the maximum time (5PM), but...
3/27/2013 at 08:15:00 <-- ...this is the most recent time because it happens the following day
In other words, you need the date so you can sort the time.
The MySQL DATETIME data type should be supported by VB.NET, but I've never used the two together so I can't guarantee it. To query and report just the time component of the date you have a ton of options. Here are two:
Query the entire date/time from MySQL and return it as a System.DateTime value to VB.NET. In VB.NET you can format it using DateTime.ToString to show only the time components. The MySQL query would go something like this:
SELECT ClientName, MAX(LastActive) AS LastActiveDateTime
FROM your_table
GROUP BY ClientName
Format the time in MySQL and return it as a String to VB.NET. In VB.NET you'll just need to display the string as is. The MySQL query would go something like this:
SELECT ClientName, DATE_FORMAT(MAX(LastActive), '%r') AS LastActiveTime
FROM your_table
GROUP BY ClientName
The format code %r in the above query will return the time in a 12-hour format with AM/PM, for example 07:55:29 PM. To return a 24-hour format (19:55:29), use %T instead.