Solved

access query

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

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
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…
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.

730 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