Access Date/Time issue

AtomCom2001

Member
Joined
Aug 2, 2007
Messages
6
Programming Experience
10+
Hi there people,

I know you must get these question all of the time, but I have to ask anyways.

I have a SQL select statement which gathers data from an ACCESS databases. It select the data via a date range from one of the fields.

VB.NET:
[QUOTE]OleCmd.CommandText = "select * from deliveries where daily_report >= #" + Format(MinDate, "dd/MM/yyyy") + "# and daily_report <= #" + Format(MaxDate, "dd/MM/yyyy") + "#"[/QUOTE]

if the date I am selecting on is like the 27/05/2007 it all works fine, problem is when the date is something like 02/06/2007 it fails...

Any ideas ???
 
I think that query should be:
VB.NET:
SELECT * FROM deliveries WHERE (daily_report >= ?) AND (daily_report <= ?)
Then you add parameters of Date type to the command and just set the dates (not strings) as parameter values.
 
JohnH may I thank you very much.

Your suggestion worked like a dream!!!

Though I did have to set the OleDbType to DBDATE instead of DATE.

Please find below the changed but now working correctly code.

VB.NET:
        OleCmd.CommandText = "select * from deliveries where (daily_report >= ?) and (daily_report <= ?)"

        OleParaCol.Add("MinDate", MinDate).OleDbType = OleDb.OleDbType.DBDate
        OleParaCol.Add("Maxdate", MinDate).OleDbType = OleDb.OleDbType.DBDate
 
if the date I am selecting on is like the 27/05/2007 it all works fine, problem is when the date is something like 02/06/2007 it fails...

Any ideas ???

Dont form your SQLs like that; it's not the right way to do it. For more info, see the PQ link in my signature. Also see the DW2 link in my signature, section on "Creating a Form to Search Data"
 
WOW!!! many many thanks..

I did not realise what a SQL coding dinosaur I was until I read that .. !! What you say makes a lot of sense and has may interesting points in it ... and I am even going to be as sad as to go over a lot of my sql statements and recoded the PROPERLY !!
 
Welcome! :)

ps; ensure you find out the parameter syntax for your chosen db

:eek:racle
@sqlserver
?mysql

access doesnt use named params, add in the order the ? appear
?

also, be aware that we dont actually write this code any more; we jsut provide the statement to the IDE and it does the coding part. See the DW2 link in my signature..
 
Back
Top