Access Sum FUnction and IIF syntax

I have a report - in a textbox control source, I have the formula -

=Sum(IIf([WkBeginDate]>=Date()-28 And [WkBeginDate]<=Date()-1,[Sales],0))

What I would like to do is make it so that it gives totals from the 4 weeks prior to the Previous Sunday every time the report is run. Is there a way to format the IIF criteria to go back to the previous SUnday - then count back 4 weeks?
dawber39Database Analyst / Application DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dsackerContract ERP Admin/ConsultantCommented:
Try this:

=Sum(IIf([WkBeginDate]>=DateAdd("d", -28, Date() - Weekday(Date()) + 1) And [WkBeginDate]<=Date()-1,[Sales],0))

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dsackerContract ERP Admin/ConsultantCommented:
Here's the breakdown:

Date() - Weekday(Date()) + 1  <-- This gives your most recent previous Sunday
DateAdd("d", -28, <the above formula>)  <-- Then gives you four weeks before that.
Gustav BrockCIOCommented:
You can use these expressions to find the first and last date:

    =DateAdd("ww", -4, DatePrevWeekday(Date(),1))

using this generic function:
Public Function DatePrevWeekday( _
  ByVal datDate As Date, _
  Optional ByVal bytWeekday As VbDayOfWeek = vbMonday) _
  As Date

' Returns the date of the previous weekday, as spelled in vbXxxxday, prior to datDate.
' 2000-09-06. Cactus Data ApS.
  ' No special error handling.
  On Error Resume Next
  DatePrevWeekday = DateAdd("d", 1 - Weekday(datDate, bytWeekday), datDate)
End Function

Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

dawber39Database Analyst / Application DeveloperAuthor Commented:
DSacker - I will give that a shot - and Gustav - I like the idea of doing it in VBA - but I have to check if that is an option with the final say. Thank you gentlemen - I shall return
dawber39Database Analyst / Application DeveloperAuthor Commented:
DSacker, It works so far - and no matter what day of the week the report is run, it will still start from the previous sunday?
dsackerContract ERP Admin/ConsultantCommented:
Yes. It subtracts the weekday, then adds 1.

So tomorrow (Tuesday) will be weekday 3. Subtracted will = Saturday. Plus 1 will = Sunday.

dawber39Database Analyst / Application DeveloperAuthor Commented:
Awesome as always
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.