views:

97

answers:

2

Hello ,,,

I have an data base (.mdb) and it has a column with dates (dd/mm/yy) , some one give me a code to get all the dates in database between 2 dates , the code was :

Select * from table where date between 'StartDate' and 'EndDate'

but after I use the code , an error occurs told me that the types of data is not the same

System.Data.OleDb.OleDbException was
 unhandled   ErrorCode=-2147217913  
 Message="عدم تطابق نوع البيانات في
 تعبير المعايير."   Source="Microsoft
 JET Database Engine"

although I convert the data type in the column of dates in database to (Date \ time) , and use OLE object to connect to data base

what is wrong , and what I have to do ?

A: 

You usually need to surround date/time types with # when working with Access, like so

#22/01/2009#
Russ Cam
I wrote code like: SQLstr = " Select * from tb where lastvstart between #0/0/0# and #30/12/2009# "but now it give me an error that it can't build the statement , Am I wrote it wrong way ?
Eias.N
You are writing it the correct way as far as I can see, but I doubt 0/0/0 is recognised as a valid date. Have you tried soemthing like `Select * from tb where lastvstart between #01/12/2009# and #30/12/2009#`? It doesn't need to necessarily return any data, we just want to know if the query is valid and does not cause an error
Russ Cam
Eias.N
A: 

Instead of using dynamic SQL, instead use the Access Database Engine's CREATE PROCEDURE SQL DDL syntax to create a persisted object whose parameters have parameters strongly-typed as DATETIME with the NULL value as default. Handle the NULL value to use the DATETIME column's value instead e.g.

CREATE PROCEDURE GetStuff
(
 arg_start_date DATETIME = NULL, 
 arg_end_date DATETIME = NULL 
)
AS
SELECT lastvstart
  FROM tb 
 WHERE lastvstart 
          BETWEEN IIF(arg_start_date IS NULL, lastvstart, arg_start_date)
          AND IIF(arg_end_date IS NULL, lastvstart, arg_end_date);
onedaywhen
I'm a bit out of touch with new features with Access- when did they introduce stored procedures?
Russ Cam
Circa 1999: Jet 4.0, Access 2000 (http://support.microsoft.com/kb/275561). Note the Access Database Engine still does not support procedural code and therefore a PROCEDURE may consist of only a single SQL statement, so some folk object to it being called a 'stored 'procedure'. What it does yield in this case, though, is: 1) stored in the DB i.e. shared by all apps using the database; 2) strongly-typed parameters; 3) default values i.e. can be omitted by the caller.
onedaywhen
thanx onedaywhen
Eias.N
This isn't a stored procedure. It's just a parameter query, which have existed in Access at least since Jet 2.x, which was released with Access 2, c. 1994.
David-W-Fenton
BTW, the DDL statement above produces this SQL, which would be completely compatible with every version of Jet/ACE I've every used: PARAMETERS arg_start_date DateTime, arg_end_date DateTime;SELECT tb.lastvstartFROM tbWHERE (((tb.lastvstart) Between IIf([arg_start_date] Is Null,[lastvstart],[arg_start_date]) And IIf([arg_end_date] Is Null,[lastvstart],[arg_end_date])));
David-W-Fenton
More to the point, what was added with the introduction of Jet 4 was not the ability to create a parameter query, but the ability to do it by DDL that was compatible with other database engines.
David-W-Fenton
@David W. Fenton: the SQL you posted doesn't show the parameters' default values so either you have made a typo or the interface you are using the examine the object (Access UI's SQL view perhaps?) is not accurately revealing the object's definition because it is not exposing the parameters' defaults.
onedaywhen