Query to get data between dates is not considering all dates

sathyaganapathi

Board Regular
Joined
Apr 29, 2021
Messages
81
Office Version
  1. 2016
Platform
  1. Windows
Hello,
I am using below query to get data from access table between two dates. But, always it is displaying dates one less than the eDate.

sDate - Start Date
eDate - end date

VBA Code:
If Me.ComboBox1.Value = "ALL" And sDate <> "" And Me.ComboBox1.Value = "ALL" And eDate <> "" Then
    qry = "SELECT * FROM TBL_PlabInput where Process_DateTime BETWEEN #" & sDate & "# AND #" & eDate & "#"
End If

for example If I select date 7/25/2021 as sDate and 7/28/2021 as eDate, data is displayed from 25th to 27th only. Data is available in database for 28th too. But it is not displayed. If both the dates are same, no data is displayed.
Please help.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
You will likely have a time element in there.? BETWEEN is fine as it is.
So 29/07/2021 12:21:00 is greater that 29/07/2021 ?

Use DateValue() function to get JUST the date element.
Hi welshgasman,
I have assigned the input as below to sDate and eDate to check the result.

from
'sDate = VBA.Format(Me.FromD.Value, "mm/dd/yyyy")
'eDate = VBA.Format(Me.ToD.Value, "mm/dd/yyyy")

to
sDate = DateValue(Me.FromD.Value)
eDate = DateValue(Me.ToD.Value)

But, still the result is same. I am missing the data for last date in the slection range. :cautious:
 
Upvote 0
So use Between edate + 1 :unsure:
Hi welshgasman,
Thanks for the input. I tried with it. It worked out !!.:)
But, still I am not clear what is wrong with the code which skips one day data? Is it not strange?:rolleyes:
what else need to be checked to get it corrected. Please help.
 
Upvote 0
| have explained it in post #8 ?
I'll try again. If you have a field with 29/07/2021 17:43 that is greater that 29/07/2021 00:00:00 which is what you have for your edate.
The time element is throwing you calc off, but when you know what is happening, then your can adjust for it.

As I mentioned you could compare to DateValue(field), then you would be comparing like for like.?
However if you add 1 to your edate, then it is good up to 29/07/2021 23:59:59 ?

Another way to think of it is
Today is really a value of 44406 as far as Access/Excel is concerned.
However as I type the date and time, Now() )is 44406.7422337963

Code:
? datevalue(date)
29/07/2021 
? cdec(date)
 44406 
? cdec(now())
 44406.7422337963

Now is it clear?
 
Upvote 0
You will likely have a time element in there.? BETWEEN is fine as it is.
So 29/07/2021 12:21:00 is greater that 29/07/2021 ?

Use DateValue() function to get JUST the date element.
Dear welsgasman,
Thanks for the input.
I used the DateValue funtion as below.
sDate = DateValue(Me.FromD.Value)
eDate = DateValue(Me.ToD.Value)
Still the problem continues. As suggested I checked after adding 1 to eDate, the result showed the last date selected.
But, the problem is, if I go with only one date for the selection, no data is fetched !
 
Upvote 0
You do NOT use it on your form controls, they already ONLY have the date element :(

You use it on the field(s). They are the ones that have the time element?

Might just be easier for you to add 1 to edate?

If you are only gong to go with one date, then build the sql string manually, or set low and high values for whatever you are not supplying?
 
Upvote 0
Solution
But, still I am not clear what is wrong with the code which skips one day data? Is it not strange?

Not even an acknowledgement either. :(
 
Upvote 0
You do NOT use it on your form controls, they already ONLY have the date element :(

You use it on the field(s). They are the ones that have the time element?

Might just be easier for you to add 1 to edate?

If you are only gong to go with one date, then build the sql string manually, or set low and high values for whatever you are not supplying?
Dear welshgasman,

Thanks for your inputs.
I could get the required result by ading one to the edate (eDate+1 in case of both the dates) and incase of only one date I did 'edate = sdate+1'. This took care of both the entries..
Thanks a lot.
 
Upvote 0
Not even an acknowledgement either. :(
Dear Micron,
Sorry, I did not notice the inputs in the flow as there was other inputs:cautious:. Extremely sorry for that. But, Later I saw your points and understood the consequences of using 'between' and 'date format'.. Thanks to you too for your valuable inputs..
Have good day.. :)
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,909
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top