MS Access form date range of single date yields no results - sql

The form uses begin date and end date text boxes formatted as "General Date" to filter results. They are generally filled using the calendar date picker tool as format m/d/yyyy. ODBC SQL table that it's pulling the date field from is also showing format "General Date" in Design View but the field includes date and time as format m/d/yyyy H:MM:SS AM/PM.
When the same date is entered in the form begin and end date it shows no results, but I am told by my team in the past it has shown results. I am assuming that this is due to the form date picker including no time and defaulting to 12 AM in both boxes when the query runs. If that is/may be the case, is there a way to edit the end date text box to default to 11:59:59 PM of the date selected?
10/28 EDIT: I was asked to provide the SQL that refers to the end date text box. For context: the command button on the form opens query dbo_GEN_INSP_Count_Daily which is based on query dbo_GEN_INSP_Count_Monthly where the text box is mentioned. See below.
query dbo_GEN_INSP_Count_Daily
SELECT DateValue([INSP_DTE]) AS Insp_Date, Count([INSP_DTE]) AS DailyCount
FROM dbo_GEN_INSP_Count_Monthly
GROUP BY DateValue([INSP_DTE]);
query dbo_GEN_INSP_Count_Monthly
SELECT dbo_VEHICLE.DMV_VIN_NUM, dbo_INSPECTION_GENERAL.INSP_DTE, dbo_INSPECTION_GENERAL.CI_NUM, dbo_INSPECTION_GENERAL.DMV_FACILITY_NUM, dbo_INSPECTION_GENERAL.VIP_UNIT_NUM
FROM dbo_INSPECTION_GENERAL INNER JOIN dbo_VEHICLE ON (dbo_INSPECTION_GENERAL.VIP_UNIT_NUM = dbo_VEHICLE.VIP_UNIT_NUM) AND (dbo_INSPECTION_GENERAL.DMV_FACILITY_NUM = dbo_VEHICLE.DMV_FACILITY_NUM) AND (dbo_INSPECTION_GENERAL.CI_NUM = dbo_VEHICLE.CI_NUM) AND (dbo_INSPECTION_GENERAL.INSP_DTE = dbo_VEHICLE.INSP_DTE)
WHERE (((dbo_INSPECTION_GENERAL.INSP_DTE) Between [Forms]![Form1]![StartDate] And [Forms]![Form1]![EndDate]));

I'm not sure how it would play with your ODBC connection, but in the date field in the form under the data tab in the property sheet there is a default value for the form, just like in the MS Access table. You could try to put your default time in there at 11:59:59, it will automatically put quotes around it and it should reflect in your form.
You could also create a default constraint on the database itself to reflect 11:59:59 for the end date if nothing is there. Going this route you would probably need to break up your time and date fields in the database and bring them together in MS Access with a function. That way if a date was placed there with no time it would default to 11:59:59.

When the user enters the same date for both StartDate and EndDate, you want the query to return all rows containing that date regardless of the time component stored with the date.
So, instead of your current Between ... And approach, make the endpoint target less than one day after EndDate.
WHERE
dbo_INSPECTION_GENERAL.INSP_DTE >= Forms!Form1!StartDate
And dbo_INSPECTION_GENERAL.INSP_DTE < (DateValue(Forms!Form1!EndDate) + 1)

Related

Access query with date BETWEEN breaks when using parameters

I have an SQL query in access that will grab all records where a calculated date is in between two values. It works fine if I hardcode date literals such as:
SELECT *
FROM Table
WHERE DateAdd("d",-60,DateAdd("yyyy",65,[Table].[BirthDate])) Between #3/21/2021# And #3/27/2021#;
However I need to parametrize the the between dates so that they can be entered by a user like:
SELECT *
FROM Table
WHERE DateAdd("d",-60,DateAdd("yyyy",65,[Table].[BirthDate])) Between [StartDate] And [EndDate];
However when I run the latter query and enter the exact same dates as the former, hard-coded one, it starts pulling records outside the between range. I've attempted to enter the dates like 3/21/2021 as well as date literals like #3/21/2021# and neither work. The latter doesn't pull anything at all.
I also have a form with a handful of text boxes using the short date format that let the user pick the dates for the query. It has the same issue of pulling back incorrect records. None of the records have any time component to my knowledge.
How can I get the date between to correctly work with user entered parameters?
Access doesn't know what data type your parameters are, so specify that in the query:
PARAMETERS
StartDate DateTime,
EndDate DateTime;
SELECT
*
FROM
Table
WHERE
DateAdd("d",-60,DateAdd("yyyy",65,[Table].[BirthDate])) Between [StartDate] And [EndDate];
Parameters worked to filter a native field - not the calculated date. However, using CDate() function worked.
SELECT *
FROM Table
WHERE DateAdd("d",-60,DateAdd("yyyy",65,[Table].[BirthDate]))
Between CDate([StartDate]) And CDate([EndDate]);
But before I remembered that, I tested calculating with inputs back to a birthdate range which also worked.
SELECT *
FROM Table
WHERE [BirthDate] Between DateAdd("d",60,DateAdd("yyyy",-65,[StartDate]))
And DateAdd("d",60,DateAdd("yyyy",-65,[EndDate]));

Removing Date from Date/Time field in Access

I'm trying to select records where the start time is between 12:00 midnight and 5:00am using Access. I use this;
Sheet1.[Start Time] BETWEEN #00:00:00# AND #04:59:59#;
But it gives me zero results. I feel it's because "Start Time" is a date/Time field and displays the date before the time. I have tried formatting the field to be a Long Time field, but it keeps the date regardless. How do I remove the date from the field?
TimeValue(MyDate) will give you just the time portion of a datetime field.

PostgreSQL date range query

I have a PSQL database that has fields for "start_time" and "end_time" in a table.
These get imported from a json file as string to a text field in the database. The string has the following format: yyyy-mm-dd hh:mm:ss
I want to do a query to give all id's of a table in the database that are within a given date range.
To do this I can do one of two things. Either I can query for a list of start and end times given a date range, and with the return from that query do a separate query for the actual data I am looking to get with WHERE start_time = ANY ('{list of dates go here}')
The other option is to figure out how to change the format of the row to something that is comparable. Something like WHERE start_time > beginning of date range AND end_time > end of date range if the fields are not text fields.
The date range is going to come from a date time picker from a website.
This might be a duplicate post but after hours of searching for a solution to either of those nothing has come up. So anything in the right direction is helpful.
Thanks
PostgreSQL should go easy on you in this case. Simply cast the string to timestamp and to the comparison.
This should work out of the box:
WHERE start_time::timestamp > '2016-01-01' AND end_time::timestamp < '2016-12-31'

how to assign User defined date in vba excel

I need to assign a date for the user to give manual date as input and make use of input at later stage. when the user inserted manual date , it has to be used for the naming conventions for the file name. Ex football 20160601.xlsx
I have gone through some website but could not able to find any answers as per my requirement.
My code:
Sub date()
date_test = Now()
Range("A1") = Format(date_test, "mm.dd.yy") ' how to aassign this date as a user defined date( as manual date) something like msg box but not msgbox.
End date
Dim inputDate as Date
Date = Format(InputBox("Enter a Date", "Input"),"yyyy-mm-dd")
Something like this?
If you want a Datepicker to select the desired date, then create a new userform and add the "Microsoft Date and Time Picker Control" (Tools->Additional Controls) to the Form. Then you can use it's value like this: Format(DatePicker1.Value,"yyyymmdd")

Records between start and end date

I am having two textfields which represents startDate and endDate.Now the problem I am facing is that I want all the records from the database which occured between this interval.But the field which stores date is TimeStamp and is of format :
24-APR-14 09.23.44.458000 PM or we can say dd-mm-yy hh.min.ss.milli AM/PM
Now obviously user entering the date is not going to enter it in such a format.So what should be query to select records from table say t1 between this date interval.
Saving Date data as Date or Datetime makes life easy.
You have tagged Mysql in the question so here is a Mysql Solution.
This is what you can do
select * from
test
where
date_format(str_to_date(`date`,'%d-%b-%y'),'%Y-%m-%d')
between '2014-04-15' AND '2014-04-24'
DEMO
You can format the user input as you want in the query in DATE_FORMAT().
User input will have a startdate and a enddate may be in datetime OR date variable
while trying to use these two input variables in the WHERE CLAUSE use BETWEEN CAST(#StartDate AS DATE) AND (CAST(#EndDate AS DATE) + 1)
This will fetch results that lies in both dates and the data range as well