Solved

access query

Posted on 2014-03-27
2
280 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
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…

948 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

23 Experts available now in Live!

Get 1:1 Help Now