Chat with a LIVE Microsoft Access Expert!
 
       
 

         

   

Go Back   Access World Forums > Microsoft Access Discussion > Queries

 
 
Chat with a LIVE Microsoft Access Expert!
Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 09-02-2007, 06:05 PM
DeanRowe DeanRowe is offline
Registered User
 
Join Date: Jan 2007
Posts: 142
DeanRowe is on a distinguished road
Exclamation querying time stamped date field

Hi,

I use a query to return values between two dates, here's the code I use:

Between [Select Start Date:] And [Select End Date:]

However because my dates are time stamped (they need to be!) the query omits anything on the end date, for example:

Between [01/09/2007] And [05/09/2007] will return values for the 1st, 2nd, 3rd, 4th but not the 5th - because (I think I'm right in thinking this but I might not be!) it only returns values upto midnight on the 4th? so 05/09/2007 13.42pm won't show up because it's after 11.59 on the 4th.

I can't ask people to enter in an extra day because quite alot of people who use our database won't remember and it'll cause alot of problems when they forget and get the wrong figures.

I've tried adding the following:
" & "11:59:59""

onto the code but Access says the expression is typed wrong or is too complicated to be evaluated.

Does anyone know how to fix this code please??

thank you for your time
Reply With Quote
Sponsored Links
  #2  
Old 09-02-2007, 07:48 PM
Moniker Moniker is offline
VBA Pro
 
Join Date: Dec 2006
Location: Dallas
Posts: 1,567
Moniker will become famous soon enoughMoniker will become famous soon enough
Between CDate([Select Start Date:]) And CDate([Select End Date:])
__________________
~Moniker

(If you've been helped by me or anyone else, please add to their reputation by clicking the "scales" icon in the upper-right.)
Reply With Quote
  #3  
Old 09-02-2007, 08:09 PM
DeanRowe DeanRowe is offline
Registered User
 
Join Date: Jan 2007
Posts: 142
DeanRowe is on a distinguished road
Hi Moniker,

Thanks for the reply - the code performed the query as normal but didn't solve the problem, tried moving the brackets and using "speech marks" but no luck. Cheers for trying tho Moniker
Reply With Quote
  #4  
Old 09-03-2007, 05:50 AM
Dennisk's Avatar
Dennisk Dennisk is offline
AWF VIP
 
Join Date: Jul 2004
Posts: 1,652
Dennisk will become famous soon enough
format the dates to remove the time component
Reply With Quote
  #5  
Old 09-03-2007, 06:34 AM
Steve R.'s Avatar
Steve R. Steve R. is offline
Formerly Ortaias
 
Join Date: Jul 2006
Location: Morehead City, North Carolina
Posts: 701
Steve R. is on a distinguished road
Two possible approaches.
1. Don't use BETWEEN instead ">=date1 AND <= Date2"
2. If you use BETWEEN use the date before and the date after. BETWEEN means between the selected time period, but NOT the limits. BETWEEN is equivalent to " > date1 AND < date2".
Reply With Quote
  #6  
Old 09-03-2007, 07:43 AM
neileg's Avatar
neileg neileg is offline
AWF VIP
 
Join Date: Dec 2002
Location: Newcastle, England
Posts: 5,970
neileg has a spectacular aura aboutneileg has a spectacular aura about
Quote:
Originally Posted by DeanRowe View Post
I've tried adding the following:
& "11:59:59""

onto the code but Access says the expression is typed wrong or is too complicated to be evaluated.
Right idea, wrong implementation. Using the ampersand and a text string turns the date entered into a string and this can't then be compared with a date. So you could:
1) Use the formula you tried but convert to a date using CDate()
2) Use DateAdd() to do proper date arithmetic to add 11:59:59
3) Use DateAdd() to add a whole day but use > < instead of between as suggested above

There are other subtle variations but that should get you started.
__________________
You can't always get what you want, but if you try sometimes you may just get what you need - Rolling Stones

Cheers, Neil
Reply With Quote
  #7  
Old 09-03-2007, 08:40 AM
Brianwarnock's Avatar
Brianwarnock Brianwarnock is offline
Retired
 
Join Date: Jun 2003
Location: Merseyside England
Posts: 7,596
Brianwarnock is a jewel in the roughBrianwarnock is a jewel in the roughBrianwarnock is a jewel in the roughBrianwarnock is a jewel in the rough
Dean you are correct in that if times are not quoted they default to 00:00:00

but surely you need to add 23:59:59 to capture everything on the last day!?
If you know that nothing ever happens at midnight, then you can use dateadd to add 1 day and use Between ..And.. but it would be more correct to use >=... And <...

Brian
__________________
What is this life if, full of care,
We have no time to stand and stare
Reply With Quote
  #8  
Old 09-03-2007, 04:45 PM
EMP EMP is offline
Registered User
 
Join Date: May 2003
Location: GT
Posts: 574
EMP is on a distinguished road
One more option. You can use the DateValue() function to extract the dates. In query SQL View, you can change the Where clause to:-

WHERE DateValue([DateTimeField]) Between [Select Start Date] And [Select End Date]


But the following should be more efficient than extracting the dates from the field:-

WHERE [DateTimeField] Between [Select Start Date] And CDATE([Select End Date])+#23:59:59#

Date/time data type is a number, so we use the + sign.

^

Last edited by EMP; 09-03-2007 at 05:22 PM..
Reply With Quote
  #9  
Old 09-04-2007, 05:10 AM
Bilbo_Baggins_Esq Bilbo_Baggins_Esq is offline
Registered User
 
Join Date: Jul 2007
Posts: 359
Bilbo_Baggins_Esq will become famous soon enough
Perhaps I'm being a little dense here, but why couldn't it just be as simple as:

Between [Select Start Date:] And ([Select End Date:]+1)


?
__________________
If you feel that a member has helped you with your question, please remember to add to that member's reputation.

Please be considerate of all users and refrain from sending direct requests for help via Private Message.
If help sessions are conducted via Private Message, not only do you limit your access to other experts, but you also limit other's access to solutions that may help them.
Reply With Quote
  #10  
Old 09-04-2007, 08:28 AM
Brianwarnock's Avatar
Brianwarnock Brianwarnock is offline
Retired
 
Join Date: Jun 2003
Location: Merseyside England
Posts: 7,596
Brianwarnock is a jewel in the roughBrianwarnock is a jewel in the roughBrianwarnock is a jewel in the roughBrianwarnock is a jewel in the rough
Bilbo you need to use CDate as per EMP, plus you would select Enddate+1 00:00:00.

I believe what you are suggesting did work prior to ACCESS 2002.

Is using CDate more efficient than Dateadd,
and Between... And ....better than>= ... And < ... ?


Brian
__________________
What is this life if, full of care,
We have no time to stand and stare

Last edited by Brianwarnock; 09-04-2007 at 08:36 AM..
Reply With Quote
  #11  
Old 09-04-2007, 08:34 AM
Bilbo_Baggins_Esq Bilbo_Baggins_Esq is offline
Registered User
 
Join Date: Jul 2007
Posts: 359
Bilbo_Baggins_Esq will become famous soon enough
Hmmmm, that's odd because I already tested it and it works terrific just as it is...

Since the date is actually numeric, adding one to the entered end date simply ensures that all date/time stamps up to midnight of that day are included.
__________________
If you feel that a member has helped you with your question, please remember to add to that member's reputation.

Please be considerate of all users and refrain from sending direct requests for help via Private Message.
If help sessions are conducted via Private Message, not only do you limit your access to other experts, but you also limit other's access to solutions that may help them.
Reply With Quote
  #12  
Old 09-04-2007, 08:41 AM
Brianwarnock's Avatar
Brianwarnock Brianwarnock is offline
Retired
 
Join Date: Jun 2003
Location: Merseyside England
Posts: 7,596
Brianwarnock is a jewel in the roughBrianwarnock is a jewel in the roughBrianwarnock is a jewel in the roughBrianwarnock is a jewel in the rough
Sorry Bilbo I just edited my post, you see I was going to suggest what EMP has done without the CDate, but being retired and nobody else suggesting it I didn't trust my memory and ran a test it didn't work, but after you suggestion dug out an old 2000 db and thats what I had [enddate]+#23:59:59#, but although it must have worked then it does not work on my 2002 system.

Brian
__________________
What is this life if, full of care,
We have no time to stand and stare
Reply With Quote
  #13  
Old 09-04-2007, 08:48 AM
Brianwarnock's Avatar
Brianwarnock Brianwarnock is offline
Retired
 
Join Date: Jun 2003
Location: Merseyside England
Posts: 7,596
Brianwarnock is a jewel in the roughBrianwarnock is a jewel in the roughBrianwarnock is a jewel in the roughBrianwarnock is a jewel in the rough
I still say that if you add 1 day and use Between....And... you are not strictly correct
Say the sates are , using English format. 01/01/07 and 31/01/07, by adding a day you will Select from 01/01/07 00:00:00 to 01/02/07 00:00:00, just as those who suggested using> and< would miss 01/01/07 00:00:00 it must be >= And <

Brian
__________________
What is this life if, full of care,
We have no time to stand and stare
Reply With Quote
Sponsored Links
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Date and Time difference Smart General 14 01-15-2007 12:02 AM
Use SELECT CASE statement in UPDATE query in VBA Curious Modules & VBA 9 10-06-2005 01:19 AM
Date / Time Calculation ramsayaj Forms 3 12-08-2002 06:17 PM
Date & Time Fixed Width Export yellow Tables 3 11-02-2002 01:07 PM
Date time field cameron Tables 1 10-03-2002 05:50 AM


All times are GMT -8. The time now is 09:49 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
(c) copyright 2009 Access World