when I tried to create the sql string is mm/dd/yyyy. How can I change
it to dd/mm/yyyy ; shall I use the format function? or do i have to
check an option in Access or regional settings in Windows?
You need to understand that date values are handled differently by VBA
and by the database engine Jet.
VBA uses the computer settings, so that something like
MyDate = CDate("01/04/2005")
will produce a January date in North America, and an April date
practically everywhere else.
Jet is completely regionally-insensitive, so that something like
WHERE MyDate = #01/04/2005#
will locate January dates everywhere in the world. If you look carefully,
when you type a date into a criterion line in the Query Designer, Access
will reformat it into a m/d/y date in the underlying SQL without asking
you. There is, however, a bug in Access that allows a line like
WHERE MyDate = #20/04/2005#
to be parsed as a d/m/y date instead of raising an error.
The bottom line is that whenever you create SQL directly you _must_ use a
Jet-compatible format: this means the USAian #mm/dd/yyyy# or the
international ISO #yyyy-mm-dd#. Also note that neither of these is what
you get if you allow VBA to do its default, therefore
"... WHERE MyDate = #" & MyDate & "#"
will fail for about 90% of the world's surface. You really do need
something like this:
"... WHERE MyDate = " & Format(MyDate, "\#yyyy\-mm\-dd\#")
and then it will always work.
There is lots more information on the usual web sites:
http://www.mvps.org/access/datetime/date0005.htm
http://www.fontstuff.com/access/acctut15.htm
.... and, just for once, the help files are pretty helpful too. Look up
Date Literals.
Hope that helps
Tim F