Solved

Access 2007 Date Query Criteria

Posted on 2014-09-10
21
287 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
  • 9
  • 7
  • 5
21 Comments
 
LVL 34

Expert Comment

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

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

Expert Comment

by:Rey Obrero
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
 
LVL 119

Expert Comment

by:Rey Obrero
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 119

Expert Comment

by:Rey Obrero
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 119

Expert Comment

by:Rey Obrero
ID: 40315277
is the name of the field with dates "datefield" ?
0
 
LVL 34

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 119

Expert Comment

by:Rey Obrero
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 34

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 34

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 34

Assisted Solution

by:PatHartman
PatHartman earned 100 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 119

Expert Comment

by:Rey Obrero
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 119

Accepted Solution

by:
Rey Obrero earned 400 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now