Solved

access query

Posted on 2014-03-27
2
279 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 325 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 49

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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