tags:

views:

33

answers:

1

I need a statement to select data from an MS Access database table.

WITHIN selected dates

I have two textboxes in my GUI called StartDate and EndDate

I want to select data within those 2 dates.

I have tried 2 methods.

The first is

" DAY(V.RegDate) between " + Start.ToString("dd") 
    + " and " + End.ToString("dd");
" and MONTH(V.RegDate) between " + Start.ToString("MM") + " and " 
    + End.ToString("MM");
" and YEAR(V.RegDate) between " + Start.ToString("yyyy") + " and " 
    + End.ToString("yyyy");

V.RegDate is the date column from the database.

But it returns me no data when I select 01/08/2010 and 01/09/2010 while there is some data at 25/08/2010.

I think that is because I chose the date separately and since the 2 dates are the same, returns me nothing

I have tried another way...

" V.RegDate between #" + Start.ToString("dd/MM/yyyy") + "# and #" _
    + End.ToString("dd/MM/yyyy") + "#";

This also return me nothing

Any Ideas????

+2  A: 

This pattern works for me:

SELECT sometable.somedate
FROM sometable
WHERE (((sometable.somedate) Between #2/1/2010# And #4/1/2010#));

(in this case it's MDY, I normally prefer ISO-ish style - YYYY/MM/DD because there is no way that Access can screw that up)

Maybe you've got your date set using the American default - MDY instead of the British(?) standard DMY.

CodeSlave
That method is ok if u choose between 01/01/2010 to31/01/2010..but not ok if u choose between 01/01/2010 to 01/02/2010..those r in dd/MM/yyyy format
william
but working fine if u choose from 01/08/2010 to 10/09/2010
william
or if u choose 01/08/2010 to 09/09/2010 not working if end date is 08/09/2010
william
SRY my bad..should b MM-dd-yyyy
william
@William - No problem.
CodeSlave
While Access is happy enough with mm-dd-yyyy, I think you will find that yyyy-mm-dd can be used everywhere and is less ambiguous in the small world in which we live.
Remou
Jet/ACE SQL requires dates in either US format (m/d/y) or in an unambiguous format like the ISO one @Remou suggests (long date is another, i.e., Format(Date(), "Long Date")).
David-W-Fenton
@Remou and @David - no disagreement here. As I said, I prefer the ISO order because it is unambiguous.
CodeSlave
I understand preferring the ISO order, but citing it doesn't actually answer the question of how to resolve the issue. There's nothing magic about the ISO format, except for the fact that it is unambiguous, as are any number of other date formats.
David-W-Fenton