Access & Dates makes my head hurt.

Comfy

Well-known Member
Joined
Dec 21, 2009
Messages
3,386
I'm extracting records to a recordset and looping through each record.

I would like clarification on something as I'm not quite sure I have the correct Query.

My Table contains a DateTime Column formatted as EU General dd/mm/yyyy hh:nn:ss (We'll use 03/05/2015 00:00:01 for this example)

I'm using this DateTime in a sub query.

SQLstr = "Select * From Table2 Where [dDate] >= #" & rSet!DateTime &"#;"

which becomes "Select * From Table2 Where [dDate] >= #03/05/2015 00:00:01#;"

Due to Access' assumption that this is a US date format mm/dd/yyyy hh:nn:ss I get the incorrect results.

To fix this I have used "Select * From Table2 Where [dDate] >= CDate('" & rSet!DateTime &"');"

which becomes "Select * From Table2 Where [dDate] >= CDate('03/05/2015 00:00:01');"

But I am not sure that this is correct.

Do I have to use DateSerial to build the correct date or would formatting the Date to mm/dd/yyyy be sufficient?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
You could try formatting to yyyy-mm-dd, which is, I think, the international standard date format.
Code:
SQLstr = "Select * From Table2 Where [dDate] >= #" & Format(rSet!DateTime, "yyyy-mm-dd") &"#;"
 
Upvote 0
try using dd/mmm/yyyy, if pulled together correctly you could have 'apr 04 2015' or '04 apr 2015' and should avoid the 2 jan / 1 feb issues
 
Upvote 0
How about
Code:
"Select * From Table2 Where CLNG([dDate]) >= CLNG('" & rSet!DateTime &"');"
or would there still be the problem that it will be converted to the wrong date in long format? I'm sure I've used this before somewhere.
 
Upvote 0
I'm not sure if ADO will like VBA conversion functions like CDate and CLng, assuming ADO is being used of course.
 
Upvote 0
Thanks Guys.

I've gone with formatting the Date column to use a Long Month so that I can be sure it's correct.

SELECT *
FROM Table1
WHERE [dDate] >= #05 Mar 2015#
AND [dDate] < #06 Mar 2015#;
 
Last edited:
Upvote 0
Date fields in Access are numbers, not text, as regards the underlying data stored in the table. Formatting should be irrelevant. It's curious what your exact problem is but I would suggest if you are using date literals in your queries to use internationally-aware date strings such as 01-Jan-2015 or Jan 1, 2015. IMHO on both sides of the atlantic we should stop using d/m/y and m/d/y -- agreeing to disagree isn't cutting it. You should also verify that the dates stored in Access are actually what you think they are (i.e., that 6/5/2015 is really 06-May-2015 - perhaps by parsing it into year, month, day or some similar technique. You probably should remain suspicious about *VBA* which is USA date-centric, but *Access* should follow your O/S locale settings. Also I would never hesitate to use DateSerial in queries to be sure as can be ... Year(date), month(date), day(date) is entirely unambiguous.
 
Last edited:
Upvote 0
Thanks Xenou.

I used dd MMM yyyy in the Table so that I can be sure the Dates are correct.

I'm confident now that my Query and results are accurate.
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,521
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top