Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more


Sorting by date

Posted on 2001-08-10
Medium Priority
Last Modified: 2006-11-17

i'm having trouble filtering data by "Date" on a subform. The situation is: I have two comboboxes and with them you specifiy the range of dates that should be displayed on the subform. Let's say that their names are: "Date1" and "Date2". I used two methods to filter, but both were not successful. First i used:

[MainForm_Subform].Form.Filter = "MYDate>=#" & Date1 & "# and MYDate<=#" & Date2 & "#"

(MYDate is the filed which contains dates)

It seemed OK, but then i noticed that when i selected let's say 1/1/00 in the first combo and 4/12/00 in the second, the records displayed were actually from 1/1/00 to 12/4/00, so the days were considered as months and vice versa.

Next i used the BuildCriteria method:

[MainForm_Subform].Form.Filter = BuildCriteria("MYDate", dbDate, ">=" & Date1 & "and <=" & Date2)

, but then something strange happened. I got the error message "You cancelled the previous operation". I was put in the debug mode and i checked what was the produced criteria like. It was:  

MYDate>=#1/1/2001#/0 And MYDate<=#12/4/2000#

I really don't know where the expression  #1/1/2001#/0  comes from, and also if that expression was correct, in the second date the months are used again as days??

What am i doing wrong??????
Question by:mario_34
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 54

Expert Comment

ID: 6371959
You're "tricked" by the combination of regional settings and the US-dateformat (mm/dd/yy) access uses.
To be 100% sure, I always use yyyy/mm/dd format when using dates between #'s, as there is no switch of day and month possible!

BTW the best filter will be using BETWEEN iso ">= AND <=", as access will perform for the BETWEEN only one compare iso two for the ">= AND <=" !


Expert Comment

ID: 6372023
nico5038, why yyyy/mm/dd ?
I can't use my locale format, since DB can't handle it, so I convert everything to US format (mm/dd/yyyy). I didn't found any document that would recommend some format...
What are your reasons for yyyy/mm/dd ?

Author Comment

ID: 6372048
Hi nico5038,

how do you obtain that the dates are stored exactly in the
yyyy/mm/dd format ?
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.


Expert Comment

ID: 6372208
I must add that I've always used the US date format, mm/dd/yyyy which, whilst it makes no sense to me as a Brit where the logical date order is dd/mm/yyyy, this always seems to work with Access. Therefore:

WHERE MyDate >= #(your date as format mm/dd/yyyy)#
AND MyDate <= #(your date as format mm/dd/yyyy)#

Or as Nico suggests, use the BETWEEN operator.

LVL 54

Expert Comment

ID: 6372313
Internally Access will store a date as a relative daynumber starting on 31-12-1899.
The way a date is seen depends on the regional settings and input-mask.
You can constructed a field looking like:
and after re-typing exactly the same date you'll get:
The yyyy-mm-dd format is safe as Access always assumes after a fourdigit year a month will follow.
Otherwise it sometimes checks the fields for being >12 and concludes then it's a day, when the value is less however it takes it as a month...

Thus I often even "drop" the date format totally and just use a number like 20010809. That sorts OK.

LVL 15

Expert Comment

ID: 6372586
I have alwaysd used date in the format #dd-mmm-yyyy#

This comes out as #12-MAR-2001# - it is recognised as a valid date by Oracle, Access, SQL Server etc.  

Author Comment

ID: 6372893

i used the following code:

[MainForm_Subform].Form.Filter = BuildCriteria("MYDate", dbDate, ">=" & Format(Date1, "dd/mm/yy") & "and <=" & Format(Date2, "dd/mm/yy"))

 and it seemed to work correctly since i brought the database to another computer. There i got an error message saying "The expression you entered has invalid . (dot) or ! operator or invalid parentheses". On the previous computer i had the date separator set to "/", but on the current it's set to ".", so i changed it to "/" and it was all OK. So, how can i make the filter indepenedent from  that settings ?

LVL 54

Expert Comment

ID: 6372943
I normally use a dash ("-") and have little problems.

BTW I would set the filter with a date-type field like:
[MainForm_Subform].Form.Filter = "MYDate BETWEEN " &  Date1 & 
" AND " & Date2
Access will do the translation then for you.
When you have stringfields use:
[MainForm_Subform].Form.Filter = "MYDate BETWEEN #" &  Date1 & 
"# AND #" & Date2 & "#"

But then the format must be OK....


Author Comment

ID: 6373648
Hi nico,

you said that you use the dash, but as i said before, the filter should be independent from this setting, because otherwise i (or the user) should change the regional settings every time the database is brought to another computer. So, is it even possible to make it independent?

LVL 54

Accepted Solution

nico5038 earned 600 total points
ID: 6373805
I tested this query:
SELECT Name, DateTest FROM tblNames WHERE (((DateTest)>#1/2/2000#));

With regional setting on ".", "-" and "/" and had no problem.
Entering e.g.:
SELECT Name, DateTest FROM tblNames WHERE (((DateTest)>#1-2-2000#));
With settings like "/" were working.
The query criteria were only displayed in the "regional setting delimiter" afterwards.

Just give it a try!


Author Comment

ID: 6374007
Hi nico!
I finally solved the problem!
I used the following syntax:

[MainForm_Subform].Form.Filter = BuildCriteria("MYDate", dbDate, ">=" & [Date1].Value & " AND <=" & [Date2].Value)

Sorry, i know you suggested me to use BETWEEN, but once it worked, i let it as it is now. Btw, the problem was in the use of brackets "[ ]". In the beggining i used this syntax:

..." & Date1 & "...

and, as i said, i always got an error message if the date separator was set to "."
Then i changed it to

..." & [Date1].Value &  "...

As you see above i also added the .Value property, even if i don't know if it's really necessary, but it works, so...

Could you please tell me what's the difference between Date1 and [Date1] .

LVL 54

Expert Comment

ID: 6374064
It should make no difference to use Date1 or [Date1], however sometimes Access will translate the text into a string....
Just check this in the criteria part of the query editor.
To force Access to see it as a field (iso textstring) the brackets ("[]") can be used. In case you have a fieldname with an intermediate space, you need to use them anyhow.

Indeed the ".value" is obsolete, as it's the default property of a field, but it's always clear when you specify it.

Just copy/paste your query and try:
[MainForm_Subform].Form.Filter = "[MYDate] BETWEEN " &  [Date1] & 
" AND " & [Date2]

This saves the "BuildCriteria" (new to me!) and it's exactly what you would specify behind the WHERE of a query.
I personally use filters and pass them as the "WHERE-part" parameter when activating a report or other form....

Glad it's working now and success with the application.


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question