Link to home
Start Free TrialLog in
Avatar of Michael Dean
Michael DeanFlag for United States of America

asked on

How to retrieve date between two dates.

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"));
ASKER CERTIFIED SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dale Fye
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])
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.
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"));
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.