?
Solved

Sorting by date

Posted on 2001-08-10
12
Medium Priority
?
191 Views
Last Modified: 2006-11-17
Hi,

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??????
0
Comment
Question by:mario_34
[X]
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
12 Comments
 
LVL 54

Expert Comment

by:nico5038
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 <=" !

Nic;o)
0
 
LVL 2

Expert Comment

by:BozzoCage
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 ?
0
 

Author Comment

by:mario_34
ID: 6372048
Hi nico5038,

how do you obtain that the dates are stored exactly in the
yyyy/mm/dd format ?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Expert Comment

by:jsweby
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.

J.
0
 
LVL 54

Expert Comment

by:nico5038
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:
 03/02/01
and after re-typing exactly the same date you'll get:
 02/03/01
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.

Nic;o)
0
 
LVL 15

Expert Comment

by:cquinn
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.  
0
 

Author Comment

by:mario_34
ID: 6372893
Hi,

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 ?



0
 
LVL 54

Expert Comment

by:nico5038
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....

Nic;o)
0
 

Author Comment

by:mario_34
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?

0
 
LVL 54

Accepted Solution

by:
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!

Nic;o)
0
 

Author Comment

by:mario_34
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] .

0
 
LVL 54

Expert Comment

by:nico5038
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.

Nic;o)
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Suggested Courses

762 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