Solved

Access 2007 Date Query Criteria

Posted on 2014-09-10
21
290 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
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)

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

863 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

24 Experts available now in Live!

Get 1:1 Help Now