Results 1 to 6 of 6
  1. #1
    jgelpi16 is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544

    Question Finding last week

    I have a bit of a dilemma. I have a query that can run any time during the week. In the table the query runs on there is a date field. What I need to do is select any records that had last week's date on it. I am struggling with this one a bit and any help will be much appreciated.

  2. #2
    jgelpi16 is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544
    Here is the SQL that I have attempted in MS Access 2007. I have limited knowledge on SQL code so I need some help with this I am sure. I get an "Invalid SQL Statement Error" on the "DECLARE" portion. Thanks a lot for your help!

    Code:
    DECLARE @TodayDayOfWeek INT
    DECLARE @EndOfPrevWeek DateTime
    DECLARE @StartOfPrevWeek DateTime
    --get number of a current day (1-Monday, 2-Tuesday... 7-Sunday)
    SET @TodayDayOfWeek = datepart(dw, GetDate())
    --get the last day of the previous week (last Sunday)
    SET @EndOfPrevWeek = DATEADD(dd, -@TodayDayOfWeek, GetDate())
    --get the first day of the previous week (the Monday before last)
    SET @StartOfPrevWeek = DATEADD(dd, -(@TodayDayOfWeek+6), GetDate())
    INSERT INTO AAtblBankExport ( [Indicator], EntityID, GarnAmt, GarnIndicator, FI, Garnish, GarnDate )
    SELECT IIf([Levy_txt]![PriEntityType]="002" Or [Levy_txt]![PriEntityType]="004","002","001") AS [Indicator], Levy_txt.PriEntityID, Sum([Levy_txt]![CaseBal]*0.01) AS GarnAmt, Levy_txt.LevyType, [Forms]![AAExport]![txtBankCode] AS FI, LevyTable.Garn, Date() AS GarnDate
    FROM Levy_txt INNER JOIN LevyTable ON Levy_txt.PriEntityID = LevyTable.PriEntityID
    WHERE (((LevyTable.GarnDate) Is Null) AND ((LevyTable.Exclude)=No)) AND ((LevyTable.ResultDate) Between CONVERT(VARCHAR, @StartOfPrevWeek,7) And CONVERT(VARCHAR, @EndOfPrevWeek+1,7))) 
    GROUP BY IIf([Levy_txt]![PriEntityType]="002" Or [Levy_txt]![PriEntityType]="004","002","001"), Levy_txt.PriEntityID, Levy_txt.LevyType, LevyTable.Garn, Levy_txt.LevySrceID
    HAVING (((LevyTable.Garn)=Yes) AND ((Levy_txt.LevySrceID)=[Forms]![AAExport]![txtBankFID]))
    ORDER BY Levy_txt.PriEntityID;
    As I have limited knowledge of SQL coding.

  3. #3
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    datePart works fine for you.

    ...datepart("ww",[datefield])=datepart("ww",now)

    add year if needed:
    and year([datefield])=year(now)

  4. #4
    jgelpi16 is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544
    What exactly does the "datePart" function do? I am thought it determines what day of the week it is. I am not sure if this is going to work. What I need is...

    Today is Tuesday. I need to find the Starting day of last week (Monday, September 13) and the ending day of last week (Friday, September 17). I want to then look for any fields in my table that have a date (probably just use the BETWEEN function). Since it could be any day of the week when the user kicks off this query I can't use a static calculation of Date() - 7 and Date() - 4.

  5. #5
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    datepart("ww",now) returns the week number of today in this year, e.g. 39 for 9/21/2011.
    if you want last week of current date, you can use:
    ...datepart("ww",[datefield])=datepart("ww",now)-1

  6. #6
    jgelpi16 is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544
    Excellent. Thank you.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Report with Running Sum by week
    By jbarrum in forum Access
    Replies: 2
    Last Post: 01-20-2010, 01:38 PM
  2. day of week input mask
    By jacko311 in forum Programming
    Replies: 7
    Last Post: 11-15-2009, 12:56 PM
  3. When Week Numbers Collide!
    By Schwagr in forum Queries
    Replies: 3
    Last Post: 03-18-2006, 10:06 PM
  4. Week number wrong
    By Nikki17 in forum Queries
    Replies: 6
    Last Post: 03-18-2006, 10:01 PM
  5. Day of Week Totals
    By ddog171 in forum Queries
    Replies: 1
    Last Post: 03-18-2006, 07:01 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums