473,503 Members | 939 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Criteria Compare dates using between and dates from other years are included

DW
I have a query in Access 2003 that has the following criteria

SELECT tblOrder.SessionDate, tblMenus.Item_Name, tblOrder.Type,
Format$(tblorder!SessionDate,"Short Time") AS SessionTime,
Sum(tblMenus.Item_Quantity) AS MenuCount INTO tblSessionQuery
FROM tblOrder, tblMenus
WHERE (((tblOrder.Order_ID)=[tblMenus].[Order_ID]) AND
((Format$([tblOrder]![SessionDate],"mm/dd/yyyy")) Between
Format$([Forms]![frmReports]![txtBeginDate],"mm/dd/yyyy") And
Format$([Forms]![frmReports]![txtEndDate],"mm/dd/yyyy")))
GROUP BY tblOrder.SessionDate, tblMenus.Item_Name, tblOrder.Type,
Format$(tblorder!SessionDate,"Short Time")
ORDER BY tblOrder.SessionDate, tblMenus.Item_Name;

If I enter the same date for both txtbegindate and txtenddate, for
example 4/6/2006 , the correct records are returned. If I enter
4/1/2006 for txtBeginDate and 4/30/2006 for txtEndDate, records that
fall within 4/1/2005 to 4/30/2005 are returned along with the 4/1/2006
- 4/30/2006 records.
I have no idea why the records from 2005 are returned. I've tried at
least dozen changes to the criteria and I can't stop the 2005 records
from being returned. I use the Format$ because without it when I enter
the same date for begin/end no records are returned. The date fields
in the table are defined using datetime data type. I've tried
identifying the query parameters in the query properties but to no
affect.

The tables are not linked. They are Access tables, not SQL server
tables.

Any suggestions are appreciated.

DW

Apr 15 '06 #1
5 5144
The Format() function returns a String. You are perform a string comparison
instead of a date comparison. Hence the dates criteria do not work as you
expect.

Suggestions:
1. Explicitly declare the parameters so Access understands the correct type.

2. If this is an Append query, JET expects the structure below (where you
fill in the fields of the target table).

3. As with the dates, you can get a time value from a field and still have
it recognised as a Date/Time value by using TimeValue().

Try something like this:

PARAMETERS [Forms]![frmReports]![txtBeginDate] DateTime,
[Forms]![frmReports]![txtEndDate] DateTime;
INSERT INTO tblSessionQuery ( F1, F2, F3, F4, F5)
SELECT tblOrder.SessionDate,
tblMenus.Item_Name,
tblOrder.Type,
TimeValue(tblorder!SessionDate) AS SessionTime,
Sum(tblMenus.Item_Quantity) AS MenuCount
FROM tblOrder INNER JOIN tblMenus
ON tblOrder.Order_ID = tblMenus.Order_ID
WHERE tblOrder.SessionDate Between
[Forms]![frmReports]![txtBeginDate]
And [Forms]![frmReports]![txtEndDate]
GROUP BY tblOrder.SessionDate, tblMenus.Item_Name,
tblOrder.Type, TimeValue(tblorder!SessionDate)
ORDER BY tblOrder.SessionDate, tblMenus.Item_Name;
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"DW" <da*********@msn.com> wrote in message
news:11*********************@t31g2000cwb.googlegro ups.com...
I have a query in Access 2003 that has the following criteria

SELECT tblOrder.SessionDate, tblMenus.Item_Name, tblOrder.Type,
Format$(tblorder!SessionDate,"Short Time") AS SessionTime,
Sum(tblMenus.Item_Quantity) AS MenuCount INTO tblSessionQuery
FROM tblOrder, tblMenus
WHERE (((tblOrder.Order_ID)=[tblMenus].[Order_ID]) AND
((Format$([tblOrder]![SessionDate],"mm/dd/yyyy")) Between
Format$([Forms]![frmReports]![txtBeginDate],"mm/dd/yyyy") And
Format$([Forms]![frmReports]![txtEndDate],"mm/dd/yyyy")))
GROUP BY tblOrder.SessionDate, tblMenus.Item_Name, tblOrder.Type,
Format$(tblorder!SessionDate,"Short Time")
ORDER BY tblOrder.SessionDate, tblMenus.Item_Name;

If I enter the same date for both txtbegindate and txtenddate, for
example 4/6/2006 , the correct records are returned. If I enter
4/1/2006 for txtBeginDate and 4/30/2006 for txtEndDate, records that
fall within 4/1/2005 to 4/30/2005 are returned along with the 4/1/2006
- 4/30/2006 records.
I have no idea why the records from 2005 are returned. I've tried at
least dozen changes to the criteria and I can't stop the 2005 records
from being returned. I use the Format$ because without it when I enter
the same date for begin/end no records are returned. The date fields
in the table are defined using datetime data type. I've tried
identifying the query parameters in the query properties but to no
affect.

The tables are not linked. They are Access tables, not SQL server
tables.

Any suggestions are appreciated.

DW

Apr 15 '06 #2
DW
Hi Mr. Browne,

Thanks for your response. I have tried declaring the parameters
explicitly.
This did not change the query results. The query is a select query,
not an append.
IF format$ returns a string, why do the 2005 dates get returned? If
these fields are returned as strings, the string 4/1/2005 is not
between the strings 4/1/2006 and 4/30/2006.
Any other suggestions?

Thanks,
David

Apr 16 '06 #3
DW
Also, if I remove the format$ function and just use between [begdate]
and [endate] the correct dates are returned. No 2005 dates. However,
if I run this query using the same date for begin and end, no records
are returned.

Thanks
DW

Apr 16 '06 #4
If no dates are returned, there is probably a time component in the
SessionDate field.

Try changing the WHERE clause so it asks for less than the next day, i.e.:

WHERE (tblOrder.SessionDate >= [Forms]![frmReports]![txtBeginDate])
And (tblOrder.SessionDate < ([Forms]![frmReports]![txtEndDate]+1))

You asked why the 2005 values are included when making a string comparision.
Strings work alphabetically, character by character. For example,
Between "goo" And "goof"
would include good and goodly, but not gopher (before) or goofy or golf
(after). The 2005 at the *end* of the string falls between the characters at
the beginning of the string. If you actually put the word 04/02/2005 into a
dictionary that is sorted alphabetically, it would come after 04/02/9999,
and before 04/03/1900.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"DW" <da*********@msn.com> wrote in message
news:11********************@v46g2000cwv.googlegrou ps.com...
Also, if I remove the format$ function and just use between [begdate]
and [endate] the correct dates are returned. No 2005 dates. However,
if I run this query using the same date for begin and end, no records
are returned.

Thanks
DW

Apr 17 '06 #5
DW

Mr Browne,

That last code did the job. This has been a very aggravating
experience.
However, I did learn alot about Access that I did not know.

Thanks again for your help,
DW

Apr 17 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5342
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
9
3916
by: Rich | last post by:
Thanks for the Help in my previous post. I've been working on this and it's almost what I want. I want to obtain the user's current age by comparing their date of birth (user inputs) to the...
2
4689
by: zeke | last post by:
Any assistance is much appreciated... I have a table with duplicate record information and I need to remove certain records based on values in four different fields. For example: PK field ...
5
6966
by: Tom | last post by:
It appears that you can't compare two dates in DotNet. You must use ToString and compare the strings. Is that the only reliable way? Try this: Dim dteOne As Date =...
12
29422
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
1
1702
by: marksham | last post by:
This title probably doesn't describe my problem very well but it's the best I can come up with. My fiscal year Starts a little before Jan1 each year, but it's an arbitrary start date, there's no...
2
5725
by: Mark Roughton | last post by:
I have a form where the users need to view records for various criteria, one of which is a date field on which they may wish to view all related data for the selected date, for all dates upto and...
2
5112
by: Ceebaby via AccessMonster.com | last post by:
Hi Folks I wondered if someone could point me in the right direction before I completely tear my hair out. I have a user selection form where options can be selected for a report. Users now...
17
5645
by: sharsy | last post by:
Hello guys, I would like some help in generating query criteria that will identify credit cards that have expired on an access database. The specific Field is formatted with a Data Type of...
0
7089
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7463
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5017
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.