?
Solved

access query

Posted on 2014-03-27
2
Medium Priority
?
290 Views
Last Modified: 2014-03-28
Hi,
 I have two field in the access table: PO_No, Entry_Date.
 I only like to list PO_No when Entry_Date is Older than 7 years from the Entry Date.
 So based on sample records below, I like list any POs that is older than or equal to 3/26/2007.
 How do I crate a query to do that?

Thanks.


PO_No                        Entry_Date
340306201      3/21/2007
0212061039      3/21/2007
340306241      3/21/2007
020207072      3/22/2007
020207073      3/22/2007
021206987      3/22/2007
021106916      3/23/2007
021106917      3/23/2007
020107033      3/24/2007
020107014      3/24/2007
490207126      3/26/2007
020107051      3/26/2007
011206976      3/26/2007
020107011      3/26/2007
020107050      3/26/2007  <---------- List this and above or older
020207081      3/27/2007
340306225      3/27/2007
020107008      3/27/2007
020207080      3/27/2007
0
Comment
Question by:sglee
2 Comments
 
LVL 13

Accepted Solution

by:
Carl Bohman earned 1300 total points
ID: 39960682
Something like this?

SELECT Table1.[PO_No], Table1.[Entry_Date]
FROM Table1
WHERE (((Table1.[Entry_Date])<DateAdd("yyyy",-7,Date())));

Open in new window

0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 39960900
You will need a proper function calculating age like the one below.
Then:

SELECT
    [PO_No],
    [Entry_Date]
FROM
    Table1
WHERE
    AgeSimple([Entry_Date]) >= 7;

Public Function AgeSimple( _
  ByVal datDateOfBirth As Date) _
  As Integer

' Returns the difference in full years from datDateOfBirth to current date.
'
' Calculates correctly for:
'   leap years
'   dates of 29. February
'   date/time values with embedded time values
'
' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28. when adding a count of years to dates of Feb. 29.
' when the resulting year is a common year.
' After an idea of Markus G. Fischer.
'
' 2007-06-26. Cactus Data ApS, CPH.

  Dim datToday  As Date
  Dim intAge    As Integer
  Dim intYears  As Integer
    
  datToday = Date
  ' Find difference in calendar years.
  intYears = DateDiff("yyyy", datDateOfBirth, datToday)
  If intYears > 0 Then
    ' Decrease by 1 if current date is earlier than birthday of current year
    ' using DateDiff to ignore a time portion of datDateOfBirth.
    intAge = intYears - Abs(DateDiff("d", datToday, DateAdd("yyyy", intYears, datDateOfBirth)) > 0)
  End If
  
  AgeSimple = intAge
  
End Function

Open in new window

/gustav
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Suggested Courses

809 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