Solved

Access 2007 Date Query Criteria

Posted on 2014-09-10
21
295 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 35

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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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 35

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 35

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 35

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 35

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Normalization of a table 19 74
sql statement - 9 25
Cascading Combo boxes between 2 sub navigation forms 1 15
Microsoft Access Reports & Sub Reports 3 14
Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
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…
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…

810 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