?
Solved

Access 2007 Date Query Criteria

Posted on 2014-09-10
21
Medium Priority
?
310 Views
Last Modified: 2014-09-10
I need help with a query on one of my tables. On one of the table fields there is a date mm/dd/yyyy. I need to have the user input a future date and then count back 20 years from the table field date.
0
Comment
Question by:CMILLER
[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
  • 9
  • 7
  • 5
21 Comments
 
LVL 39

Expert Comment

by:PatHartman
ID: 40315030
Use DateAdd() to subtract 20 years from the entered date.

NewDate = DateAdd("yyyy", -20, YourDate)
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40315031
try this format

SELECT ID, DateField
FROM YourTable
WHERE [DateField]=DateAdd("yyyy",-20,[Enter Date])
0
 

Author Comment

by:CMILLER
ID: 40315178
Rey,

its not working. here is the format for the date field mm/dd/yyyy
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40315184
post the query you are using.

is the field type Date/Time? or text?

better if you can upload a copy of your db.
0
 

Author Comment

by:CMILLER
ID: 40315234
its date/time

sorry, cant upload
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40315238
post the query you are using.
0
 

Author Comment

by:CMILLER
ID: 40315273
SELECT [table1].datefield
FROM [table1]
WHERE ((([table1].[datefield])=DateAdd("yyyy",-20,[Enter Date])));
0
 

Author Comment

by:CMILLER
ID: 40315275
it asks for the date but doesnt return anything
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40315277
is the name of the field with dates "datefield" ?
0
 
LVL 39

Expert Comment

by:PatHartman
ID: 40315280
Are you sure there is a record with the date you are looking for?
Is datefield defined as a Date data type?
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40315296
see this sample db,

enter 1/1/2014 and the query will return records with date 1/1/2013
Q-28515613.accdb
0
 

Author Comment

by:CMILLER
ID: 40315300
I am not looking for a specific date that matches -20 years.

I want to be able to enter a date ( 01/14/2015 ) and find all records or dates that go back +20 years.

so, it would find ALL dates that are before 01/14/1995
0
 

Author Comment

by:CMILLER
ID: 40315315
how do I download the accdb file? I have ran across this before. You can upload one but i think there is an issue with EE on downloading accdb files. am I correct?
0
 
LVL 39

Expert Comment

by:PatHartman
ID: 40315346
Then you can't use = as the relational operator.  Your query is looking for a specific date.  Between will probably get what you want.

WHERE ((([table1].[datefield]) Between DateAdd("yyyy",-20,[Enter Date]))) AND [Enter Date];
0
 
LVL 39

Expert Comment

by:PatHartman
ID: 40315352
I've complained about the download issue but not gotten a resolution.  Since I can't normally download files anyway, I never pushed the issue but sometimes it is nice to actually see what everyone is talking about.
0
 

Author Comment

by:CMILLER
ID: 40315381
I am getting an error "between operator without And in query expression"
0
 
LVL 39

Assisted Solution

by:PatHartman
PatHartman earned 400 total points
ID: 40315449
It probably has to do with all the excess parentheses.  Get rid of all them except the ones needed for the DateAdd() function.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40315573
<so, it would find ALL dates that are before 01/14/1995 >

would have been a lot easier if you clarified this criteria


run query1
Q-28515613.mdb
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 1600 total points
ID: 40315582
sorry use this, run query2
Q-28515613.mdb
0
 

Author Comment

by:CMILLER
ID: 40315586
Rey, I apologize that it wasnt clear.
0
 

Author Comment

by:CMILLER
ID: 40315594
Rey, the second query worked.

Thanks.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

719 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