Connecting Tech Pros Worldwide Help | Site Map

Start/End Date Query Problems when end date isn't <=

Newbie
 
Join Date: Mar 2008
Posts: 4
#1: Mar 23 '08
Hi, I’m trying to set up a query to find all dates on or between a start and end date.

Table name and field names I’ve used:
Tbl_bookings
Booking No
Boarding Arrival Date
Boarding Departure Date

So far I’ve been using the criteria:
>=[Enter the arrival date]And<=[Enter the departure date]
under the arrival date field.

The SQL for the query is as follows:
Expand|Select|Wrap|Line Numbers
  1. SELECT tbl_bookings.[Booking No], tbl_bookings.[Boarding Arrival Date], tbl_bookings.[Boarding Departure Date]
  2. FROM tbl_bookings
  3. WHERE (((tbl_bookings.[Boarding Arrival Date])>=[Enter Arrival Date] And (tbl_bookings.[Boarding Arrival Date])<=[Enter Departure Date]));
The problem that I’m having is when I enter a set of two dates and the departure date is greater than the departure date stored in the bookings table.

For example the test data I’m currently using is as follows:
Test Data
Expand|Select|Wrap|Line Numbers
  1. Booking No    Arrives      Departs
  2.     2       01/01/2008   08/01/2008
  3.     5       07/01/2008   12/01/2008
  4.     6       07/01/2008   12/01/2008
  5.     7       07/01/2008   12/01/2008
  6.     9       09/02/2008   14/02/2008
  7.    10       09/02/2008   14/02/2008
I enter the dates 01/01/2008 to 07/01/2008 in the parameter value boxes and bookings 2,5,6,7 are returned as expected.

However if I enter 06/01/2008 to13/01/2008 only bookings 5,6,7 are returned yet booking 2 should also have been

I have the same problem again entering values such as 10/01/2008 to 11/02/2008, bookings 9,10 are returned but not 5,6,7.

I quite new to using access and was wondering if someone could point me in the right direction as to how to develop this query so that it works for when the departure date entered is not less than or equal to dates that are stored in the system.

Many Thanks
Scott Price's Avatar
Moderator
 
Join Date: Jul 2007
Location: Seattle, WA
Posts: 1,314
#2: Mar 23 '08

re: Start/End Date Query Problems when end date isn't <=


Have a good look through this article in our How-tos section.

You are running into a very common problem with SQL and the Access Jet engine interpreting dates in a certain way.

If you notice the dates that you say are assessed correctly, they are all 'ambiguous'... i.e. 07/01/2008 can mean July 1, 2008 (in North America) or 7 January, 2008 (in Great Britain). What is happening is that the SQL engine always look for the North American format, and interprets in this format.

You will need to pass your date values through at least the CDate() function, and possibly the Format() function first in order to get this to evaluate correctly.

The syntax will be: CDate([YourDateField) If this doesn't work try: CDate(Format([YourDateField], "m/d/yyyy"))

Regards,
Scott
Newbie
 
Join Date: Mar 2008
Posts: 4
#3: Mar 25 '08

re: Start/End Date Query Problems when end date isn't <=


Quote:

Originally Posted by Scott Price

Have a good look through this article in our How-tos section.

You are running into a very common problem with SQL and the Access Jet engine interpreting dates in a certain way.

If you notice the dates that you say are assessed correctly, they are all 'ambiguous'... i.e. 07/01/2008 can mean July 1, 2008 (in North America) or 7 January, 2008 (in Great Britain). What is happening is that the SQL engine always look for the North American format, and interprets in this format.

You will need to pass your date values through at least the CDate() function, and possibly the Format() function first in order to get this to evaluate correctly.

The syntax will be: CDate([YourDateField) If this doesn't work try: CDate(Format([YourDateField], "m/d/yyyy"))

Regards,
Scott

Hi, Thanks for your help,

I've had a look through the article you mentioned and understand about the dates being interpreted wrongly and needing to use the CDate function to correct this.

I've had a play around the syntax you suggested but can't seem to get it to work. I am still quite new to this so probably going about it the wrong way. Do i need to use the CDate function individually for both my start and end date or together? And do i need to be using this within the SQL view of the query or the criteria in the design view of the query builder?

These are probably really obvious questions but ive been trying with different ways and don't seem to be getting anywhere.

Any help is much appreciated!
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,714
#4: Mar 26 '08

re: Start/End Date Query Problems when end date isn't <=


Millie,

Post in what you're currently using and explain where and when it goes wrong.
We can look at it for you and help you to resolve your issues.

CDate() is not generally necessary but the actual circumstances need to be known before we can judge the best way to proceed.

We will probably need to know the field types of any that are used too.
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,714
#5: Mar 26 '08

re: Start/End Date Query Problems when end date isn't <=


I maybe should have looked more closely at your original post :/

Anyway, try the Between X And Y syntax in your SQL :
Expand|Select|Wrap|Line Numbers
  1. SELECT [Booking No],
  2.        [Boarding Arrival Date],
  3.        [Boarding Departure Date]
  4. FROM tbl_bookings
  5. WHERE [Boarding Arrival Date] Between
  6.       [Enter Arrival Date] And
  7.       [Enter Departure Date];
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,714
#6: Mar 26 '08

re: Start/End Date Query Problems when end date isn't <=


Looking yet again, and this may seem like a dumb question, but aren't you really after a selection where the record spans a single date entered?

That would be more like :
Expand|Select|Wrap|Line Numbers
  1. SELECT [Booking No],
  2.        [Boarding Arrival Date],
  3.        [Boarding Departure Date]
  4. FROM tbl_bookings
  5. WHERE [Enter Date] Between
  6.       [Boarding Arrival Date] And
  7.       [Boarding Departure Date];
Newbie
 
Join Date: Mar 2008
Posts: 4
#7: Mar 26 '08

re: Start/End Date Query Problems when end date isn't <=


Hi Thanks for your help!
I've tried using a Between statement before and tried it again as you suggested but it still isn't doing what i want it to do. I also played around with your 2nd suggestion.

What i want is for the user to be able to enter a proposed arrival date and departure date for a booking she is about to make. I then want the query to return all bookings already stored in the system, for and between the two dates she enters.

The problem i seem to be having though is when the user would enter for example: 06/01/2008 to 13/01/2008
the query brings back bookings 5,6,7 from my test data (as seen in my first post). But not booking no 2. However booking 2 doesn't depart untill 08/01/2008 so would be there for two of days, in the period of dates entered by the user. So would expect booking 2 to be returned.

Thanks again for your help, i beginning to whether there is actually no way around this.
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,714
#8: Mar 26 '08

re: Start/End Date Query Problems when end date isn't <=


Actually, this question is coming up so frequently recently I may have to write an article on it.

It's certainly possible, and involves checking that both the following are true :
  1. [Proposed End Date] >= [Start Date] (of existing data).
  2. [Proposed Start Date] <= [End Date] (of existing data).
Go away and play with that for a while, and if you can't manage to get it to work then come back and we can fill in the details for you.
Newbie
 
Join Date: Mar 2008
Posts: 4
#9: Mar 27 '08

re: Start/End Date Query Problems when end date isn't <=


Hi again,

It's working!!! Thanks so much for all your help, i've been trying to sort this out for ages but wasn't getting anywhere. It's now working for the examples i'd been using before and i've tested it with other sets of dates and it works just how i wanted it to!

Thanks again,
Millie
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,714
#10: Mar 27 '08

re: Start/End Date Query Problems when end date isn't <=


Very pleased to hear it Millie.

Not so sure about all the surprise though - "Oh ye of little faith" :D

PS. You know I'm just kidding around right :)
Reply


Similar Microsoft Access / VBA bytes