How to retrieve date between two dates.

seamus9909
seamus9909 used Ask the Experts™
on
I have a query below, when the user enters on form1

10/1/2017       [text63] which is the earliest date
10/31/2017     [text65] which is the latest date

The results do not include 10/31 they only show up to 10/30.

What is the syntax to be used to include all rows that are from one to to and including another date?




WHERE (((tblClaims.InvoiceSentDate) Between [Forms]![form1]![text63] And [Forms]![form1]![text65]) AND ((Customers.Company)='USAA INSURANCE COMPANY')) OR (((Customers.Company)="Schwartz Law Firm, PC")) OR (((Customers.Company)="Garan, Lucow, Miller, PC"));
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Database Expert
Awarded 2016
Top Expert 2016
Commented:
Please try this-

WHERE (((tblClaims.InvoiceSentDate) >= [Forms]![form1]![text63] And
(tblClaims.InvoiceSentDate) <= [Forms]![form1]![text65]) AND ((Customers.Company)='USAA INSURANCE COMPANY')) OR (((Customers.Company)="Schwartz Law Firm, PC")) OR (((Customers.Company)="Garan, Lucow, Miller, PC"));
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Commented:
Chances are that your [SentDate] field includes a time component.  I generally use:

WHERE DateValue(tblClaims.InvoiceSentDate) >= DateValue([Forms]![form1]![text63])
AND DateValue(tblClaims.InvoiceSentDate) <= DateValue([Forms]![form1]![text65])
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Commented:
but you probably don't need the DateValue referring to your form controls.
WHERE DateValue(tblClaims.InvoiceSentDate) >= cdate([Forms]![form1]![text63])
AND DateValue(tblClaims.InvoiceSentDate) <= cdate([Forms]![form1]![text65]) 

Open in new window

This assumes you have other code that would require that text63 and text65 contain values.  Otherwise, you might want to use the NZ() function to force dates into those criteria.

BTW.  I strongly recommend a control naming convention txt_FromDate, txt_ThruDate instead of accepting the default control names that Access assigns to controls on your form.  It is much easier to write code and debug it when the control names have meaning.
Pawan KumarDatabase Expert
Awarded 2016
Top Expert 2016

Commented:
Updated. Please try . No need to put DATEVALUE around texts are they are dates only.

WHERE ((DATEVALUE(tblClaims.InvoiceSentDate) Between [Forms]![form1]![text63] And [Forms]![form1]![text65]) AND ((Customers.Company)='USAA INSURANCE COMPANY')) OR (((Customers.Company)="Schwartz Law Firm, PC")) OR (((Customers.Company)="Garan, Lucow, Miller, PC"));

OR

WHERE ((DATEVALUE(tblClaims.InvoiceSentDate) >= [Forms]![form1]![text63] And
DATEVALUE(tblClaims.InvoiceSentDate) <= [Forms]![form1]![text65]) AND ((Customers.Company)='USAA INSURANCE COMPANY')) OR (((Customers.Company)="Schwartz Law Firm, PC")) OR (((Customers.Company)="Garan, Lucow, Miller, PC"));
Distinguished Expert 2017

Commented:
Another solution, which avoids the use of VBA functions in the  WHERE clause which can adversely affect performance:

WHERE tblClaims.InvoiceSentDate >= [Forms]![form1]![text63]
AND tblClaims.InvoiceSentDate < [Forms]![form1]![text65] +1

PS - best practice is to actually name controls and forms with meaningful names.  You aren't going to know next week what text65 is let alone next year.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial