Stop calculating passed days when a status changes

megatronixs

Registered User.
Local time
Today, 13:52
Joined
Aug 17, 2012
Messages
719
Code:
Hi all,

I have a function that check how many days passed since a request was started. It will stop whe the finalized date is entered. Now I need to make a change that when a new status is set, it should stop counting the days passed and only start counting days passed when another status is selected. If a request was started like 12 working days ago and the status is changed to "Awaiting Documentation" then, it should stop and only start counting again when the next status is changed to "Documents Received".
I added a new column to the database that I called "status_stop", that maybe could hold a date when the "Awaiting Documentation" was entered and that would then discount it from the passed days. But to be honest, I have no idea how to pull this one.

This is the code I have so far:
Code:
review_duration: CInt(ReviewDuration(Nz([tbl_main_data].[request_date],0),Nz([tbl_main_data].[finalized_date],0)))

And these are the functions:
Code:
Option Explicit
Option Compare Database
Function DateDiffWorkDays(BegDate As Date, EndDate As Date) As Integer
'Returns number of days, excluding Saturday and Sunday
'As written, counts both BegDate and EndDate, e.g.,
'Monday - Friday would count as 5 days
'coded by: raskew
Const SATURDAY = 6
Const SUNDAY = 7
Dim NumWeeks As Integer
If BegDate = 0 Then
    DateDiffWorkDays = 0
    
    Exit Function
End If
Select Case Weekday(BegDate, vbMonday)
Case SATURDAY: BegDate = BegDate + 2
Case SUNDAY: BegDate = BegDate + 1
End Select
Select Case Weekday(EndDate, vbMonday)
Case SATURDAY: EndDate = EndDate - 1
Case SUNDAY: EndDate = EndDate - 2
End Select
If BegDate > EndDate Then
    DateDiffWorkDays = 0
    
    Exit Function
End If
NumWeeks = (EndDate - BegDate - ((EndDate - BegDate) Mod 7)) / 7
    If Weekday(EndDate, vbMonday) >= Weekday(BegDate, vbMonday) Then
    
        DateDiffWorkDays = NumWeeks * 5 - Weekday(BegDate, vbMonday) + Weekday(EndDate, vbMonday) + 1
    
    Else
    
        DateDiffWorkDays = NumWeeks * 5 + 5 - Weekday(BegDate, vbMonday) + Weekday(EndDate, vbMonday) + 1
    
    End If
End Function

Function ReviewDuration(Optional Request As Date, Optional finalized As Date)
'TURNING finalized INTO TODAY'S DATE IF FINALIZED WAS MISSING
If finalized = 0 Then
finalized = Date
End If
'PRE-CONDITIONS
 
If Request = 0 Or _
    Request > finalized Or _
    Request > Date Then
    
    ReviewDuration = 0
    
    Exit Function
End If
'IF FINALIZED IS EMPTY (nz condition in query) THEN FINALIZED = TODAY'S DATE
If finalized = 0 Then
finalized = Date
End If
'CALCULATIONS
Dim intFullWeeksPassed As Integer
Dim intDaysCalculated As Integer
Dim intInsideWeekDifference As Integer

Select Case Weekday(Request, vbMonday)
Case 6
Request = Request + 2
Case 7
Request = Request + 1
End Select
Select Case Weekday(finalized, vbMonday)
Case 6
finalized = finalized - 1
Case 7
finalized = finalized - 2
End Select
'making check for situations, in which both dates were entered in the same weekend
If Request > finalized Then
ReviewDuration = 0
Exit Function
End If
'full weeks passed
intFullWeeksPassed = ((finalized - Request) - ((finalized - Request) Mod 7)) / 7
'calculation when finalized weekday >= request weekday or finalized weekday < request weekday
If Weekday(finalized, vbMonday) >= Weekday(Request, vbMonday) Then
intInsideWeekDifference = Weekday(finalized, vbMonday) - Weekday(Request, vbMonday)
Else
intInsideWeekDifference = 5 + Weekday(finalized, vbMonday) - Weekday(Request, vbMonday)
End If
' final calculation
ReviewDuration = intFullWeeksPassed * 5 + intInsideWeekDifference + 1
ReviewDuration = CInt(ReviewDuration)
Exit Function
ErrorHandler:
ReviewDuration = 0
End Function

Greetings.
formula in the query:
 
in a query, create a field that calcs the days:

IIF(IsNull([finalDate],DateDiff("d",[startDate],Date()),DateDiff("d",[startDate],[FinalDate])
 
Hi Ranman256,
It is telling me that it is missing a closing parenthesis, bracket or vertical bar.
I tried all possible things, but can't find the mistake there.

Greetings.
 
hi, still no solution for this, looks so strange and I can't figure it out.

Greetings.
 
Try:

IIF(IsNull([finalDate],DateDiff("d",[startDate],Date())),DateDiff("d",[startDate],[FinalDate]))
 
Hi Bob Fitz,

I will check this as soon as I have the chance.

Greetings.
 
But the other thing is that if you have an object with multiple dated "status change" events whose dates need to be retained, it would be most efficient to solve this with a related table, not by adding additional status fields--and their dates--to the main table.
 
MarkK, you beat me to it.

Megatronixs, I'm going to toss out one of my "old programmer's rules" that is relevant to this process: Access won't tell you anything you didn't tell it first.

What that means is that if you needed to capture the number of days that your object is in some particular state before its state gets changed to something else, then you need to track status changes and dates. Access won't tell you SQUAT unless you tell it what it needs to be able to answer that question - AND you have to TRACK that info, which implies the existence of a state table that contains not less than: Object ID, date of status change, Status Code. Could contain more, but these three are minimum for the ability you described.

With such a table, the status of the object is merely the status entered at the status change with the most recent date, and it is not much of a query to do that. If needed, it could even be done via DLookup, perhaps.
 
Hi all,

I attached a test database. It will be easier to explain this way.
The database we use, is like a helping database. There is the big brother where all data is stored and updated.There all the status changes are captured with time stamp. The helping database is just to have everything in a much simpler view and just the current status that will be changed if the case goes to the next step.
In the examples in the attached database, case ID 2, is started and the status is set to documents received. Now the counting should stop and only and continue when the status changes to differnend one (like Documents received) Instead of having 3 days passed, it should be 2 days passed as the status was changed yesterday.

I hope this will be more of a help.

Greetings.
 

Attachments

How do we describe and manage the status of a "thing" without the "thing" itself being represented in the system? There is only one table here, and it looks like all status information. But the status of what?
 
Hi MarkK,

I managed to kind of solve it. I added a second column to the database.
now I have to date fields to capture the [stop_status] and the [status_go_on]
When a status is changed to "Awaiting Documentation" the first date is filled in. When the next status is changed to "Documention Received" the second date is filled in. this way I will see how many days passed from one status to the other status.
I simply have to use the 2 functions from the query and if one case has 20 days open and the stop_status and status_go_on is 5 days, the outcome will be 15 days passed instead of 20.

I have now a simply VBA that will use the after_update from the combobox where some one selects the data. But it is possible that the status could come from updated table from the master database. This case the status changes will not be triggerd as it is done manually.

How could I create a function that checks the changes in the those columns?
something like a "Select Case"
The combobox that you can select manually the status is called combo_status
and the updates could come in the morning.

Greetings.
 
When you add a new "thing" you should be adding a new row to a table. If you combine multiple things in a single row, it will always be a fight to separate them later.
 
Hi MarkK,

Those dates are captured each on his on column.
I still have problems with the function to fill in the date based on the status, but will open a new thread for that one.

Greetings.
 

Users who are viewing this thread

Back
Top Bottom