views:

173

answers:

2

I have the following query set up, in various incarcerations, to remind me of contract expirations 0, 7, 30, 60, and 90 days before the confirmed expiration date. My method of calculating the notification date doesn't give me any flexibility. For example, say I have 30 contracts expiring 12/5, a Saturday when my office isn't open. I will miss the notification that the contract expired that day, and 7 days out.

SELECT qryAllSold.SoldID, qryAllSold.CompanyName, qryAllSold.Product, qryAllSold.Description, qryAllSold.Quantity, qryAllSold.Price, qryAllSold.ConfirmedExpires, qryAllSold.Note
FROM qryAllSold
WHERE (((qryAllSold.ConfirmedExpires)=DateAdd("d",0,Int(Now()))) AND ((qryAllSold.RenewedToID) Is Null) AND ((qryAllSold.NonOpportunity)=No));

Each of these queries will be displayed on one "dashboard" form to serve as a daily task list. It would be great if I could have the form represent a date, and click an arrow to move forward or back a day at a time, but I don't know if Access has that capability.

If that won't work, what other ways can I make sure I don't miss notifications on these expirations?

+2  A: 

[EDIT] If you are not a programmer here is an alternative solution

Why not use the Format function in your query, e.g.

Format([ConfirmedExpires],"ww",2,1)

"ww" - specifies weeks, 2 specifies Monday as first day of the week, 1 means week 1 contains 1 Jan

Now it is easy to do date calculations in weeks e.g. Contract expiring more than 4 weeks into the future:

Format([ConfirmedExpires],"ww",2,1)-Format(Now(),"ww",2,1))>4

Contracts expiring more than 4 weeks into the future and up to 5 weeks into the future

Format([ConfirmedExpires],"ww",2,1)-Format(Now(),"ww",2,1))>4 -
Format([ConfirmedExpires],"ww",2,1)-Format(Now(),"ww",2,1)),=5

Does that help?


Why don't you just use the weekday feature of functions like DateAdd?

Instead of calculating 30 calendar days ahead you could calculate 25, or any other number of weekdays ahead.

The code below displays a message box but you could equally use the function in a SQL query behind a message box.

Sub TestWeekDay()
    Dim FirstDate As Date    ' Declare variables.
    Dim IntervalType As String
    Dim Number As Integer
    Dim Msg

    IntervalType = "ww"    ' "ww" specifies weeks as interval.
    FirstDate = InputBox("Enter a date")
    Number = InputBox("Enter number of weekdays to add")
    Msg = "New date: " & DateAdd(IntervalType, Number, FirstDate)
    MsgBox Msg
End Sub
heferav
I thought about this, but will Sunday - 1 W give me Friday? How will this allow for holidays?
NickSentowski
If your contracts are financial e.g. FX, commodity, etc. then contracts will specify that to what to do if settlement falls on Sat, Sun, or a public holiday. Your program should follow the documentation.Sun - 1w = Friday because the function goes backwards to the first working day it finds. This is what is needed most of the time i.e. you settle transactions on the Friday before the weekend.
heferav
Where do I insert this in my SQL?Sry, I'm a salesman writing my own tools, not a programmer.
NickSentowski
I have reworded my solution so that it can be done without code
heferav
+1  A: 

Perhaps a little complication:

IIf(Weekday(DateAdd("d", 0, Date())) = 7, DateAdd("d", 0, Date()) + 2, IIf(Weekday(DateAdd("d", 0, Date())) = 1, DateAdd("d", 0, Date()) + 1, DateAdd("d", 0, Date())))

This can be put in your where statement, like so:

SELECT q.SoldID, q.CompanyName, q.Product, 
       q.Description, q.Quantity, q.Price, 
       q.ConfirmedExpires, q.Note
FROM qryAllSold AS q
WHERE q.ConfirmedExpires=IIf(Weekday(DateAdd("d", 0, Date())) = 7, DateAdd("d", 0, Date()) + 2, IIf(Weekday(DateAdd("d", 0, Date())) = 1, DateAdd("d", 0, Date()) + 1, DateAdd("d", 0, Date())))
AND q.RenewedToID Is Null AND q.NonOpportunity=No

Note that I have used the alias q for qryAllSold as this makes it easier to read the query: FROM qryAllSold AS q

It would probably be best to use False instead of No.

If you wish, you should be able to use a parameter:

PARAMETERS NoDays Short;
SELECT q.SoldID, q.CompanyName, q.Product, 
       q.Description, q.Quantity, q.Price, 
       q.ConfirmedExpires, q.Note
FROM qryAllSold AS q
WHERE q.ConfirmedExpires=IIf(Weekday(DateAdd("d",[NoDays],Date()))=7,DateAdd("d",[NoDays],Date())+2,IIf(Weekday(DateAdd("d",[NoDays],Date()))=1,DateAdd("d",[NoDays],Date())+1,DateAdd("d",[NoDays],Date())))
AND q.RenewedToID Is Null AND q.NonOpportunity=No

The parameter allows you to pick a number of days. It would be even better if this referred to a field on a form or was set in VBA.

Remou
I really don't know how that's going to help me. Am I reading it wrong? It doesn't look like something that will work in an SQL query.Here's how I'm reading it in pseudo:If today+0=7 then today+0+2Else IF today+0=1 then today+0+1 Else Today+0 EndifEndif
NickSentowski
If today = day 7 of the week (Saturday where week begins on Sunday), date + 2 (Monday), if today = day 1 of week (Sunday), date + 1 (Monday), else today.
Remou
Where do I insert this in my SQL? Sry, I'm a salesman writing my own tools, not a programmer.
NickSentowski
I have added a comment. You should also be able to use a parameter for number of days.
Remou
I tried giving you both the checkmark, because I like both responses... I'm only giving preference to heferav because he seems to need the rep more.Thanks mate!
NickSentowski