Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Access 2007 Date Query Criteria

Posted on 2014-09-10
21
Medium Priority
?
317 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 40

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
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 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 40

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 40

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 40

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 40

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

824 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