Query multiple Dates

C

Connie

I have two tables T_Actions and T_CaseNbr. They are related by the field
“CaseNbrâ€. There can be many actions for one
CaseNbr. The actions are input on a datasheet subform linked to the main
CaseNbr Form using Parent/Child CaseNbr.

The Actions are dated with the Now() function when they are originated. The
fields are ActionDate, Dept, Status, Sent, Initials.

I want to make a report that will show the number of days between each
action for a case number. I tried a query that had a MAX and MIN
ActionDate, but that doesn’t get me the days between each action. (it could
also be Hours, Min, etc). Since the fields are all called
ActionDate, how can I subtract ActionDate From ActionDate?

It seems simple, but I can’t seem to get the report I need. The object of
the report is to see how long it takes for a particular department to perform
an Action, then pass it to another Dept, they do THEIR Action and pass it to
the Next Dept. The final step is the last Dept sends the “Action†to print
and marks the Action “Completeâ€.

I hope someone can make sense of this and help me find a way to do this.

Thanks,
Connie
 
B

bin_leigh

DateDiff is your friend. You can return d as days and so on.

Type DateDiff and press return in help for all the choices.

The thing to remember its the difference between two dates
t_actions.actionDate and t_caseNbr.actionDate.... so a couple of days = 48
hours... do you see.

You need to actually find the smallest nominator, seconds by the sound of
it, and keep dividing up. 190 seconds / 60 = 3..... 3*60 = 180..... 190-180
= 10 seconds, hence 3 minutes 10 seconds. Are you sure you want to do this!?
 
C

Connie

I would be satisfied if the difference being anything less than 1 day
defaulted to 0 days, then if that wasn't close enough I will deal with
hours/min/sec. But I'm still a little unclear as to how to set this up for
a report. Do I first make a query using both tables T_CaseNbr and T_Actions.
List the T_Action.ActionDate But there isn't a T__CaseNbr.ActionDate ??
The link field is "CaseNbr". How do I get to the DateDiff you described?

Hope I'm not being too dense on this one... <smile>
Connie
 
K

KARL DEWEY

The Actions are dated with the Now() function when they are originated.
The Now() function include a time component, not just date.

Do you have a ActionDate field in each of the tables?
 
C

Connie

No. The ActionDate is only in the T_Action Table. The link between the two
is the CaseNbr. Many Actions to One Case Number.

The reason for the Now() function in the ActionDate is so that it would show
what times during the day that the "Action" was done and passed to the next
person.

The goal is to see how long it takes between actions and then also total
days(?) between the first action and the "complete" action.

Am I explaining it OK?
Thanks,
Connie
 
B

bin_leigh

This is an exciting one...

I think I know what you're after, but had to make a quick mock up... I hope
your SQL is OK!

I'm only illustrating one difference, you would need a query on a query for
each Action type. I've created your two tables and only called my actions
"1" and "2". but its expandable.

First query is:

SELECT T_CaseNbr.CaseNbr, T_CaseNbr.blah, T_Actions.Status, T_Actions.date,
T_Actions_1.Status, T_Actions_1.date
FROM (T_CaseNbr INNER JOIN T_Actions AS T_Actions_1 ON T_CaseNbr.CaseNbr =
T_Actions_1.CaseNbr) INNER JOIN T_Actions ON T_CaseNbr.CaseNbr =
T_Actions.CaseNbr
WHERE (((T_Actions.Status)="1") AND ((T_Actions_1.Status)="2"));

Summary: blah is just a field... whatever you're collecting in the CaseNbr
table... its not really needed here. The key aspect is I've added T_Actions
table in twice to query... Access creates the name T_Actions_1 automatically.
You have to manually drag the T_CaseNbr Primary Key across to the second
table... T_Actions_1

For the first T_Action table I set the column criteria for Status as "1"...
the first action... for the 2nd T_Action table I set the column criteria for
Status to "2"... so I'm selecting all Cases with a Status of 1 and a Status
of 2... ones not at this stage aren't selected.

We move on to query number 2.

SELECT Query1.CaseNbr, Query1.blah,
DateDiff("d",Query1![T_Actions.date],Query1![T_Actions_1.date]) AS
difference, Query1.T_Actions.date, Query1.T_Actions_1.date
FROM Query1;

Much simpler... but so am I.

No tables (you might need more data for your report though) just the query1.

Summary, I've now gathered my data... so I can now manipulate it or use it
for calculations, hence the two steps instead of one.

Column name: DataDiff("d", StartDate, EndDate)

My startDate is Query1![T_Actions.date]
My endDate is Query1![T_Actions_1.date]

I could have renamed the columns in query1 but I don't think it would be so
clear.

Let me know how you get on... I've only wine for company.

Cheers.
 
C

Connie

WOW! Thanks for taking the time to analyze this for me! Now, I need to
print this out and study it. I think I was on the right track - I had two
queries, but hadn't attacked it like you did.... and I didn't quite know what
to do after the two queries. I actually built to temporary tables to house
the info....then got stuck and came here.
So, now I will review, but probably won't return until tomorrow at work again.

I can't thank you enough for putting me on the right track. Let's hope we
can use that wine to celebrate a victory...this issue is KEY to do what we
need.

Thanks,thanks,thanks,
Connie
 
C

Connie

Ok. I studied this last night and tried to apply it to my situation. I'm
not sure how this will work.
1. I don't know how many Actions I will have for each CaseNbr. Do I
have to make a copy of the T_Action table in the first query for EACH Action?
Each CaseNbr record will be different ?? So How many do I create?
2. T_Action.Status is a field already used for information. I can't
use it for criteria like "1". (So I created a new field in the table to try
to substitute for it in the formula... I called it "ActNbr".) So your formula
now would read "T_Actions_1.ActNbr = 1".

3. Now, I'm confused as to where to go because I need an answer to my
question 1. How many of these tables do I need to create in Query 1?

If you can help me here to continue, I'll keep going. Thanks for sticking
with me on this. If you can think of a cleaner way to track these actions
I'm also open to changing the structure of my tables at this point.

Thanks once again for your help.
Connie





Connie said:
WOW! Thanks for taking the time to analyze this for me! Now, I need to
print this out and study it. I think I was on the right track - I had two
queries, but hadn't attacked it like you did.... and I didn't quite know what
to do after the two queries. I actually built to temporary tables to house
the info....then got stuck and came here.
So, now I will review, but probably won't return until tomorrow at work again.

I can't thank you enough for putting me on the right track. Let's hope we
can use that wine to celebrate a victory...this issue is KEY to do what we
need.

Thanks,thanks,thanks,
Connie

bin_leigh said:
This is an exciting one...

I think I know what you're after, but had to make a quick mock up... I hope
your SQL is OK!

I'm only illustrating one difference, you would need a query on a query for
each Action type. I've created your two tables and only called my actions
"1" and "2". but its expandable.

First query is:

SELECT T_CaseNbr.CaseNbr, T_CaseNbr.blah, T_Actions.Status, T_Actions.date,
T_Actions_1.Status, T_Actions_1.date
FROM (T_CaseNbr INNER JOIN T_Actions AS T_Actions_1 ON T_CaseNbr.CaseNbr =
T_Actions_1.CaseNbr) INNER JOIN T_Actions ON T_CaseNbr.CaseNbr =
T_Actions.CaseNbr
WHERE (((T_Actions.Status)="1") AND ((T_Actions_1.Status)="2"));

Summary: blah is just a field... whatever you're collecting in the CaseNbr
table... its not really needed here. The key aspect is I've added T_Actions
table in twice to query... Access creates the name T_Actions_1 automatically.
You have to manually drag the T_CaseNbr Primary Key across to the second
table... T_Actions_1

For the first T_Action table I set the column criteria for Status as "1"...
the first action... for the 2nd T_Action table I set the column criteria for
Status to "2"... so I'm selecting all Cases with a Status of 1 and a Status
of 2... ones not at this stage aren't selected.

We move on to query number 2.

SELECT Query1.CaseNbr, Query1.blah,
DateDiff("d",Query1![T_Actions.date],Query1![T_Actions_1.date]) AS
difference, Query1.T_Actions.date, Query1.T_Actions_1.date
FROM Query1;

Much simpler... but so am I.

No tables (you might need more data for your report though) just the query1.

Summary, I've now gathered my data... so I can now manipulate it or use it
for calculations, hence the two steps instead of one.

Column name: DataDiff("d", StartDate, EndDate)

My startDate is Query1![T_Actions.date]
My endDate is Query1![T_Actions_1.date]

I could have renamed the columns in query1 but I don't think it would be so
clear.

Let me know how you get on... I've only wine for company.

Cheers.
 
C

Connie

One more time... (I haven't let you answer inbetween...sorry)<smile> But I
think I've figured out my own questions. I still had to do some minuplating
in the report, but I finally got the results I needed. I have to say I'm
amazed that the way to get the difference between two dates in the same table
in the manner I need, was harder than I thought. I wonder if anyone has a
better way of storing the data that would prevent all the extra work to find
the times between one action and the next action, then the time from THAT
action to the NEXT Action, and soforth....

Thanks, tho, and here are the two queries as they ended up:

SELECT T_CaseNbr.CaseNbr, T_Actions.StatusDesc, T_Actions.ActionDate AS
StartDate, T_Actions_1.ActionDate AS EndDate
FROM (T_CaseNbr INNER JOIN T_Actions ON T_CaseNbr.CaseNbr =
T_Actions.CaseNbr) INNER JOIN T_Actions AS T_Actions_1 ON T_CaseNbr.CaseNbr =
T_Actions_1.CaseNbr;


SELECT Q_NEW_ACTIONS.CaseNbr, Q_NEW_ACTIONS.StatusDesc,
Q_NEW_ACTIONS.StartDate, Q_NEW_ACTIONS.EndDate,
DateDiff("d",[StartDate],[EndDate]) AS DAYSBTWN
FROM Q_NEW_ACTIONS;

Then in the actual report, I had to blank all duplicates, sort by StartDate,
Place the Data in the footer of the StatusDesc. This is a similar look to
my report:

CASE NBR: 07890M
Sent to Enrollment Dept 8/1/05 10:00:00 AM
Sent to Enrollment Supv 8/1/05 10:30:00 AM
Sent to Processing Dept 8/3/05 9:30:00 AM 2
Sent to Processing Supv 8/4/05 8:00:00 AM 1
Sent to Accting Dept 8/4/05 9:00:00 AM
Sent to Accting Supv 8/5/05 1:00:04 PM 1
Sent to Print Chk 8/6/05 7:00:00 AM
1
Check Mailed 8/7/05 8:00:00 AM
1

Total Days from Beg
6

This not only shows the full tracking, but will also show the areas that the
process slows down.

Once again, I say thanks... and if anyone has a cleaner or better way of
handling
this, please feel free to comment.
Connie

bin_leigh said:
This is an exciting one...

I think I know what you're after, but had to make a quick mock up... I hope
your SQL is OK!

I'm only illustrating one difference, you would need a query on a query for
each Action type. I've created your two tables and only called my actions
"1" and "2". but its expandable.

First query is:

SELECT T_CaseNbr.CaseNbr, T_CaseNbr.blah, T_Actions.Status, T_Actions.date,
T_Actions_1.Status, T_Actions_1.date
FROM (T_CaseNbr INNER JOIN T_Actions AS T_Actions_1 ON T_CaseNbr.CaseNbr =
T_Actions_1.CaseNbr) INNER JOIN T_Actions ON T_CaseNbr.CaseNbr =
T_Actions.CaseNbr
WHERE (((T_Actions.Status)="1") AND ((T_Actions_1.Status)="2"));

Summary: blah is just a field... whatever you're collecting in the CaseNbr
table... its not really needed here. The key aspect is I've added T_Actions
table in twice to query... Access creates the name T_Actions_1 automatically.
You have to manually drag the T_CaseNbr Primary Key across to the second
table... T_Actions_1

For the first T_Action table I set the column criteria for Status as "1"...
the first action... for the 2nd T_Action table I set the column criteria for
Status to "2"... so I'm selecting all Cases with a Status of 1 and a Status
of 2... ones not at this stage aren't selected.

We move on to query number 2.

SELECT Query1.CaseNbr, Query1.blah,
DateDiff("d",Query1![T_Actions.date],Query1![T_Actions_1.date]) AS
difference, Query1.T_Actions.date, Query1.T_Actions_1.date
FROM Query1;

Much simpler... but so am I.

No tables (you might need more data for your report though) just the query1.

Summary, I've now gathered my data... so I can now manipulate it or use it
for calculations, hence the two steps instead of one.

Column name: DataDiff("d", StartDate, EndDate)

My startDate is Query1![T_Actions.date]
My endDate is Query1![T_Actions_1.date]

I could have renamed the columns in query1 but I don't think it would be so
clear.

Let me know how you get on... I've only wine for company.

Cheers.
 
C

Connie

HELP! This ended up NOT working. I still can't get the difference between
two (or more) ActionDates.

Could someone take another look at this and see if you can help.
Thanks so much!!
Connie

Connie said:
One more time... (I haven't let you answer inbetween...sorry)<smile> But I
think I've figured out my own questions. I still had to do some minuplating
in the report, but I finally got the results I needed. I have to say I'm
amazed that the way to get the difference between two dates in the same table
in the manner I need, was harder than I thought. I wonder if anyone has a
better way of storing the data that would prevent all the extra work to find
the times between one action and the next action, then the time from THAT
action to the NEXT Action, and soforth....

Thanks, tho, and here are the two queries as they ended up:

SELECT T_CaseNbr.CaseNbr, T_Actions.StatusDesc, T_Actions.ActionDate AS
StartDate, T_Actions_1.ActionDate AS EndDate
FROM (T_CaseNbr INNER JOIN T_Actions ON T_CaseNbr.CaseNbr =
T_Actions.CaseNbr) INNER JOIN T_Actions AS T_Actions_1 ON T_CaseNbr.CaseNbr =
T_Actions_1.CaseNbr;


SELECT Q_NEW_ACTIONS.CaseNbr, Q_NEW_ACTIONS.StatusDesc,
Q_NEW_ACTIONS.StartDate, Q_NEW_ACTIONS.EndDate,
DateDiff("d",[StartDate],[EndDate]) AS DAYSBTWN
FROM Q_NEW_ACTIONS;

Then in the actual report, I had to blank all duplicates, sort by StartDate,
Place the Data in the footer of the StatusDesc. This is a similar look to
my report:

CASE NBR: 07890M
Sent to Enrollment Dept 8/1/05 10:00:00 AM
Sent to Enrollment Supv 8/1/05 10:30:00 AM
Sent to Processing Dept 8/3/05 9:30:00 AM 2
Sent to Processing Supv 8/4/05 8:00:00 AM 1
Sent to Accting Dept 8/4/05 9:00:00 AM
Sent to Accting Supv 8/5/05 1:00:04 PM 1
Sent to Print Chk 8/6/05 7:00:00 AM
1
Check Mailed 8/7/05 8:00:00 AM
1

Total Days from Beg
6

This not only shows the full tracking, but will also show the areas that the
process slows down.

Once again, I say thanks... and if anyone has a cleaner or better way of
handling
this, please feel free to comment.
Connie

bin_leigh said:
This is an exciting one...

I think I know what you're after, but had to make a quick mock up... I hope
your SQL is OK!

I'm only illustrating one difference, you would need a query on a query for
each Action type. I've created your two tables and only called my actions
"1" and "2". but its expandable.

First query is:

SELECT T_CaseNbr.CaseNbr, T_CaseNbr.blah, T_Actions.Status, T_Actions.date,
T_Actions_1.Status, T_Actions_1.date
FROM (T_CaseNbr INNER JOIN T_Actions AS T_Actions_1 ON T_CaseNbr.CaseNbr =
T_Actions_1.CaseNbr) INNER JOIN T_Actions ON T_CaseNbr.CaseNbr =
T_Actions.CaseNbr
WHERE (((T_Actions.Status)="1") AND ((T_Actions_1.Status)="2"));

Summary: blah is just a field... whatever you're collecting in the CaseNbr
table... its not really needed here. The key aspect is I've added T_Actions
table in twice to query... Access creates the name T_Actions_1 automatically.
You have to manually drag the T_CaseNbr Primary Key across to the second
table... T_Actions_1

For the first T_Action table I set the column criteria for Status as "1"...
the first action... for the 2nd T_Action table I set the column criteria for
Status to "2"... so I'm selecting all Cases with a Status of 1 and a Status
of 2... ones not at this stage aren't selected.

We move on to query number 2.

SELECT Query1.CaseNbr, Query1.blah,
DateDiff("d",Query1![T_Actions.date],Query1![T_Actions_1.date]) AS
difference, Query1.T_Actions.date, Query1.T_Actions_1.date
FROM Query1;

Much simpler... but so am I.

No tables (you might need more data for your report though) just the query1.

Summary, I've now gathered my data... so I can now manipulate it or use it
for calculations, hence the two steps instead of one.

Column name: DataDiff("d", StartDate, EndDate)

My startDate is Query1![T_Actions.date]
My endDate is Query1![T_Actions_1.date]

I could have renamed the columns in query1 but I don't think it would be so
clear.

Let me know how you get on... I've only wine for company.

Cheers.
 
M

MDI Anne

Connie, I know exactly what you're trying to get. I'm having the same
problem also. I have multiple events for one filing number (ie received,
letter sent, resubmitted, sent to legal, received from legal, closed) and
each of these events has a date to it. The dates are typically more than one
day apart, so a -1 datediff doesn't help. But I know there "has" to be a way
of counting the days between each event.

Surely someone out there can give us a glimmer of hope!


Connie said:
HELP! This ended up NOT working. I still can't get the difference between
two (or more) ActionDates.

Could someone take another look at this and see if you can help.
Thanks so much!!
Connie

Connie said:
One more time... (I haven't let you answer inbetween...sorry)<smile> But I
think I've figured out my own questions. I still had to do some minuplating
in the report, but I finally got the results I needed. I have to say I'm
amazed that the way to get the difference between two dates in the same table
in the manner I need, was harder than I thought. I wonder if anyone has a
better way of storing the data that would prevent all the extra work to find
the times between one action and the next action, then the time from THAT
action to the NEXT Action, and soforth....

Thanks, tho, and here are the two queries as they ended up:

SELECT T_CaseNbr.CaseNbr, T_Actions.StatusDesc, T_Actions.ActionDate AS
StartDate, T_Actions_1.ActionDate AS EndDate
FROM (T_CaseNbr INNER JOIN T_Actions ON T_CaseNbr.CaseNbr =
T_Actions.CaseNbr) INNER JOIN T_Actions AS T_Actions_1 ON T_CaseNbr.CaseNbr =
T_Actions_1.CaseNbr;


SELECT Q_NEW_ACTIONS.CaseNbr, Q_NEW_ACTIONS.StatusDesc,
Q_NEW_ACTIONS.StartDate, Q_NEW_ACTIONS.EndDate,
DateDiff("d",[StartDate],[EndDate]) AS DAYSBTWN
FROM Q_NEW_ACTIONS;

Then in the actual report, I had to blank all duplicates, sort by StartDate,
Place the Data in the footer of the StatusDesc. This is a similar look to
my report:

CASE NBR: 07890M
Sent to Enrollment Dept 8/1/05 10:00:00 AM
Sent to Enrollment Supv 8/1/05 10:30:00 AM
Sent to Processing Dept 8/3/05 9:30:00 AM 2
Sent to Processing Supv 8/4/05 8:00:00 AM 1
Sent to Accting Dept 8/4/05 9:00:00 AM
Sent to Accting Supv 8/5/05 1:00:04 PM 1
Sent to Print Chk 8/6/05 7:00:00 AM
1
Check Mailed 8/7/05 8:00:00 AM
1

Total Days from Beg
6

This not only shows the full tracking, but will also show the areas that the
process slows down.

Once again, I say thanks... and if anyone has a cleaner or better way of
handling
this, please feel free to comment.
Connie

bin_leigh said:
This is an exciting one...

I think I know what you're after, but had to make a quick mock up... I hope
your SQL is OK!

I'm only illustrating one difference, you would need a query on a query for
each Action type. I've created your two tables and only called my actions
"1" and "2". but its expandable.

First query is:

SELECT T_CaseNbr.CaseNbr, T_CaseNbr.blah, T_Actions.Status, T_Actions.date,
T_Actions_1.Status, T_Actions_1.date
FROM (T_CaseNbr INNER JOIN T_Actions AS T_Actions_1 ON T_CaseNbr.CaseNbr =
T_Actions_1.CaseNbr) INNER JOIN T_Actions ON T_CaseNbr.CaseNbr =
T_Actions.CaseNbr
WHERE (((T_Actions.Status)="1") AND ((T_Actions_1.Status)="2"));

Summary: blah is just a field... whatever you're collecting in the CaseNbr
table... its not really needed here. The key aspect is I've added T_Actions
table in twice to query... Access creates the name T_Actions_1 automatically.
You have to manually drag the T_CaseNbr Primary Key across to the second
table... T_Actions_1

For the first T_Action table I set the column criteria for Status as "1"...
the first action... for the 2nd T_Action table I set the column criteria for
Status to "2"... so I'm selecting all Cases with a Status of 1 and a Status
of 2... ones not at this stage aren't selected.

We move on to query number 2.

SELECT Query1.CaseNbr, Query1.blah,
DateDiff("d",Query1![T_Actions.date],Query1![T_Actions_1.date]) AS
difference, Query1.T_Actions.date, Query1.T_Actions_1.date
FROM Query1;

Much simpler... but so am I.

No tables (you might need more data for your report though) just the query1.

Summary, I've now gathered my data... so I can now manipulate it or use it
for calculations, hence the two steps instead of one.

Column name: DataDiff("d", StartDate, EndDate)

My startDate is Query1![T_Actions.date]
My endDate is Query1![T_Actions_1.date]

I could have renamed the columns in query1 but I don't think it would be so
clear.

Let me know how you get on... I've only wine for company.

Cheers.
 
C

Connie R via AccessMonster.com

Thanks for letting me know I'm not alone Anne,

I'm still hunting for an answer. I know this can't be so difficult!!! What
am I missing here? HELP!!
Anne, is your database set up with all the "events" in one table, and all the
Filing numbers in another table? Is the link between the two, the filing
number?

Is the field named EventDate? Because if each of the events have a different
name, I would think you could use the following module: Just cut and paste
into a new module, name it something other than Diff2Dates, then use it like
you would any other function. i.e. Diff2Dates("dhn",[Date1],[Date2],0)
It works GREAT!!!


'***************** Code Start **************
Public Function Diff2Dates(Interval As String, Date1 As Date, Date2 As Date,
_
Optional ShowZero As Boolean = False) As Variant
'Author: © Copyright 2001 Pacific Database Pty Limited
' Graham R Seach MCP MVP [email protected]
' Phone: +61 2 9872 9594 Fax: +61 2 9872 9593
' This code is freeware. Enjoy...
' (*) Amendments suggested by Douglas J. Steele MVP
'
'Description: This function calculates the number of years,
' months, days, hours, minutes and seconds between
' two dates, as elapsed time.
'
'Inputs: Interval: Intervals to be displayed (a string)
' Date1: The lower date (see below)
' Date2: The higher date (see below)
' ShowZero: Boolean to select showing zero elements
'
'Outputs: On error: Null
' On no error: Variant containing the number of years,
' months, days, hours, minutes & seconds between
' the two dates, depending on the display interval
' selected.
' If Date1 is greater than Date2, the result will
' be a negative value.
' The function compensates for the lack of any intervals
' not listed. For example, if Interval lists "m", but
' not "y", the function adds the value of the year
' component to the month component.
' If ShowZero is True, and an output element is zero, it
' is displayed. However, if ShowZero is False or
' omitted, no zero-value elements are displayed.
' For example, with ShowZero = False, Interval = "ym",
' elements = 0 & 1 respectively, the output string
' will be "1 month" - not "0 years 1 month".

On Error GoTo Err_Diff2Dates

Dim booCalcYears As Boolean
Dim booCalcMonths As Boolean
Dim booCalcDays As Boolean
Dim booCalcHours As Boolean
Dim booCalcMinutes As Boolean
Dim booCalcSeconds As Boolean
Dim booSwapped As Boolean
Dim dtTemp As Date
Dim intCounter As Integer
Dim lngDiffYears As Long
Dim lngDiffMonths As Long
Dim lngDiffDays As Long
Dim lngDiffHours As Long
Dim lngDiffMinutes As Long
Dim lngDiffSeconds As Long
Dim varTemp As Variant

Const INTERVALS As String = "dmyhns"

'Check that Interval contains only valid characters
Interval = LCase$(Interval)
For intCounter = 1 To Len(Interval)
If InStr(1, INTERVALS, Mid$(Interval, intCounter, 1)) = 0 Then
Exit Function
End If
Next intCounter

'Check that valid dates have been entered
If Not (IsDate(Date1)) Then Exit Function
If Not (IsDate(Date2)) Then Exit Function

'If necessary, swap the dates, to ensure that
'Date1 is lower than Date2
If Date1 > Date2 Then
dtTemp = Date1
Date1 = Date2
Date2 = dtTemp
booSwapped = True
End If

Diff2Dates = Null
varTemp = Null

'What intervals are supplied
booCalcYears = (InStr(1, Interval, "y") > 0)
booCalcMonths = (InStr(1, Interval, "m") > 0)
booCalcDays = (InStr(1, Interval, "d") > 0)
booCalcHours = (InStr(1, Interval, "h") > 0)
booCalcMinutes = (InStr(1, Interval, "n") > 0)
booCalcSeconds = (InStr(1, Interval, "s") > 0)

'Get the cumulative differences
If booCalcYears Then
lngDiffYears = Abs(DateDiff("yyyy", Date1, Date2)) - _
IIf(Format$(Date1, "mmddhhnnss") <= Format$(Date2, "mmddhhnnss")
, 0, 1)
Date1 = DateAdd("yyyy", lngDiffYears, Date1)
End If

If booCalcMonths Then
lngDiffMonths = Abs(DateDiff("m", Date1, Date2)) - _
IIf(Format$(Date1, "ddhhnnss") <= Format$(Date2, "ddhhnnss"), 0,
1)
Date1 = DateAdd("m", lngDiffMonths, Date1)
End If

If booCalcDays Then
lngDiffDays = Abs(DateDiff("d", Date1, Date2)) - _
IIf(Format$(Date1, "hhnnss") <= Format$(Date2, "hhnnss"), 0, 1)
Date1 = DateAdd("d", lngDiffDays, Date1)
End If

If booCalcHours Then
lngDiffHours = Abs(DateDiff("h", Date1, Date2)) - _
IIf(Format$(Date1, "nnss") <= Format$(Date2, "nnss"), 0, 1)
Date1 = DateAdd("h", lngDiffHours, Date1)
End If

If booCalcMinutes Then
lngDiffMinutes = Abs(DateDiff("n", Date1, Date2)) - _
IIf(Format$(Date1, "ss") <= Format$(Date2, "ss"), 0, 1)
Date1 = DateAdd("n", lngDiffMinutes, Date1)
End If

If booCalcSeconds Then
lngDiffSeconds = Abs(DateDiff("s", Date1, Date2))
Date1 = DateAdd("s", lngDiffSeconds, Date1)
End If

If booCalcYears And (lngDiffYears > 0 Or ShowZero) Then
varTemp = lngDiffYears & IIf(lngDiffYears <> 1, " years", " year")
End If

If booCalcMonths And (lngDiffMonths > 0 Or ShowZero) Then
If booCalcMonths Then
varTemp = varTemp & IIf(IsNull(varTemp), Null, " ") & _
lngDiffMonths & IIf(lngDiffMonths <> 1, " months", "
month")
End If
End If

If booCalcDays And (lngDiffDays > 0 Or ShowZero) Then
If booCalcDays Then
varTemp = varTemp & IIf(IsNull(varTemp), Null, " ") & _
lngDiffDays & IIf(lngDiffDays <> 1, " days", " day")
End If
End If

If booCalcHours And (lngDiffHours > 0 Or ShowZero) Then
If booCalcHours Then
varTemp = varTemp & IIf(IsNull(varTemp), Null, " ") & _
lngDiffHours & IIf(lngDiffHours <> 1, " hours", " hour")
End If
End If

If booCalcMinutes And (lngDiffMinutes > 0 Or ShowZero) Then
If booCalcMinutes Then
varTemp = varTemp & IIf(IsNull(varTemp), Null, " ") & _
lngDiffMinutes & IIf(lngDiffMinutes <> 1, " minutes", "
minute")
End If
End If

If booCalcSeconds And (lngDiffSeconds > 0 Or ShowZero) Then
If booCalcSeconds Then
varTemp = varTemp & IIf(IsNull(varTemp), Null, " ") & _
lngDiffSeconds & IIf(lngDiffSeconds <> 1, " seconds", "
second")
End If
End If

If booSwapped Then
varTemp = "-" & varTemp
End If

Diff2Dates = Trim$(varTemp)

End_Diff2Dates:
Exit Function

Err_Diff2Dates:
Resume End_Diff2Dates

End Function
'************** Code End *****************



MDI said:
Connie, I know exactly what you're trying to get. I'm having the same
problem also. I have multiple events for one filing number (ie received,
letter sent, resubmitted, sent to legal, received from legal, closed) and
each of these events has a date to it. The dates are typically more than one
day apart, so a -1 datediff doesn't help. But I know there "has" to be a way
of counting the days between each event.

Surely someone out there can give us a glimmer of hope!
HELP! This ended up NOT working. I still can't get the difference between
two (or more) ActionDates.
[quoted text clipped - 106 lines]
 
M

MDI Anne

Connie, my database is setup with all of the events in one table and the
filing numbers on another table, linked by the filing number. In my event
table I have EVENT_CODE and EVENT_DATE. All of my events are listed under
the event code and the corresponding dates in the event date column. I just
want to know the time lapsed between each event for each filing, but can't
find a way to manipulate the previous or next record to get the info I want.


Connie R via AccessMonster.com said:
Thanks for letting me know I'm not alone Anne,

I'm still hunting for an answer. I know this can't be so difficult!!! What
am I missing here? HELP!!
Anne, is your database set up with all the "events" in one table, and all the
Filing numbers in another table? Is the link between the two, the filing
number?

Is the field named EventDate? Because if each of the events have a different
name, I would think you could use the following module: Just cut and paste
into a new module, name it something other than Diff2Dates, then use it like
you would any other function. i.e. Diff2Dates("dhn",[Date1],[Date2],0)
It works GREAT!!!


'***************** Code Start **************
Public Function Diff2Dates(Interval As String, Date1 As Date, Date2 As Date,
_
Optional ShowZero As Boolean = False) As Variant
'Author: © Copyright 2001 Pacific Database Pty Limited
' Graham R Seach MCP MVP [email protected]
' Phone: +61 2 9872 9594 Fax: +61 2 9872 9593
' This code is freeware. Enjoy...
' (*) Amendments suggested by Douglas J. Steele MVP
'
'Description: This function calculates the number of years,
' months, days, hours, minutes and seconds between
' two dates, as elapsed time.
'
'Inputs: Interval: Intervals to be displayed (a string)
' Date1: The lower date (see below)
' Date2: The higher date (see below)
' ShowZero: Boolean to select showing zero elements
'
'Outputs: On error: Null
' On no error: Variant containing the number of years,
' months, days, hours, minutes & seconds between
' the two dates, depending on the display interval
' selected.
' If Date1 is greater than Date2, the result will
' be a negative value.
' The function compensates for the lack of any intervals
' not listed. For example, if Interval lists "m", but
' not "y", the function adds the value of the year
' component to the month component.
' If ShowZero is True, and an output element is zero, it
' is displayed. However, if ShowZero is False or
' omitted, no zero-value elements are displayed.
' For example, with ShowZero = False, Interval = "ym",
' elements = 0 & 1 respectively, the output string
' will be "1 month" - not "0 years 1 month".

On Error GoTo Err_Diff2Dates

Dim booCalcYears As Boolean
Dim booCalcMonths As Boolean
Dim booCalcDays As Boolean
Dim booCalcHours As Boolean
Dim booCalcMinutes As Boolean
Dim booCalcSeconds As Boolean
Dim booSwapped As Boolean
Dim dtTemp As Date
Dim intCounter As Integer
Dim lngDiffYears As Long
Dim lngDiffMonths As Long
Dim lngDiffDays As Long
Dim lngDiffHours As Long
Dim lngDiffMinutes As Long
Dim lngDiffSeconds As Long
Dim varTemp As Variant

Const INTERVALS As String = "dmyhns"

'Check that Interval contains only valid characters
Interval = LCase$(Interval)
For intCounter = 1 To Len(Interval)
If InStr(1, INTERVALS, Mid$(Interval, intCounter, 1)) = 0 Then
Exit Function
End If
Next intCounter

'Check that valid dates have been entered
If Not (IsDate(Date1)) Then Exit Function
If Not (IsDate(Date2)) Then Exit Function

'If necessary, swap the dates, to ensure that
'Date1 is lower than Date2
If Date1 > Date2 Then
dtTemp = Date1
Date1 = Date2
Date2 = dtTemp
booSwapped = True
End If

Diff2Dates = Null
varTemp = Null

'What intervals are supplied
booCalcYears = (InStr(1, Interval, "y") > 0)
booCalcMonths = (InStr(1, Interval, "m") > 0)
booCalcDays = (InStr(1, Interval, "d") > 0)
booCalcHours = (InStr(1, Interval, "h") > 0)
booCalcMinutes = (InStr(1, Interval, "n") > 0)
booCalcSeconds = (InStr(1, Interval, "s") > 0)

'Get the cumulative differences
If booCalcYears Then
lngDiffYears = Abs(DateDiff("yyyy", Date1, Date2)) - _
IIf(Format$(Date1, "mmddhhnnss") <= Format$(Date2, "mmddhhnnss")
, 0, 1)
Date1 = DateAdd("yyyy", lngDiffYears, Date1)
End If

If booCalcMonths Then
lngDiffMonths = Abs(DateDiff("m", Date1, Date2)) - _
IIf(Format$(Date1, "ddhhnnss") <= Format$(Date2, "ddhhnnss"), 0,
1)
Date1 = DateAdd("m", lngDiffMonths, Date1)
End If

If booCalcDays Then
lngDiffDays = Abs(DateDiff("d", Date1, Date2)) - _
IIf(Format$(Date1, "hhnnss") <= Format$(Date2, "hhnnss"), 0, 1)
Date1 = DateAdd("d", lngDiffDays, Date1)
End If

If booCalcHours Then
lngDiffHours = Abs(DateDiff("h", Date1, Date2)) - _
IIf(Format$(Date1, "nnss") <= Format$(Date2, "nnss"), 0, 1)
Date1 = DateAdd("h", lngDiffHours, Date1)
End If

If booCalcMinutes Then
lngDiffMinutes = Abs(DateDiff("n", Date1, Date2)) - _
IIf(Format$(Date1, "ss") <= Format$(Date2, "ss"), 0, 1)
Date1 = DateAdd("n", lngDiffMinutes, Date1)
End If

If booCalcSeconds Then
lngDiffSeconds = Abs(DateDiff("s", Date1, Date2))
Date1 = DateAdd("s", lngDiffSeconds, Date1)
End If

If booCalcYears And (lngDiffYears > 0 Or ShowZero) Then
varTemp = lngDiffYears & IIf(lngDiffYears <> 1, " years", " year")
End If

If booCalcMonths And (lngDiffMonths > 0 Or ShowZero) Then
If booCalcMonths Then
varTemp = varTemp & IIf(IsNull(varTemp), Null, " ") & _
lngDiffMonths & IIf(lngDiffMonths <> 1, " months", "
month")
End If
End If

If booCalcDays And (lngDiffDays > 0 Or ShowZero) Then
If booCalcDays Then
varTemp = varTemp & IIf(IsNull(varTemp), Null, " ") & _
lngDiffDays & IIf(lngDiffDays <> 1, " days", " day")
End If
End If

If booCalcHours And (lngDiffHours > 0 Or ShowZero) Then
If booCalcHours Then
varTemp = varTemp & IIf(IsNull(varTemp), Null, " ") & _
lngDiffHours & IIf(lngDiffHours <> 1, " hours", " hour")
End If
End If

If booCalcMinutes And (lngDiffMinutes > 0 Or ShowZero) Then
If booCalcMinutes Then
varTemp = varTemp & IIf(IsNull(varTemp), Null, " ") & _
lngDiffMinutes & IIf(lngDiffMinutes <> 1, " minutes", "
minute")
End If
End If

If booCalcSeconds And (lngDiffSeconds > 0 Or ShowZero) Then
If booCalcSeconds Then
varTemp = varTemp & IIf(IsNull(varTemp), Null, " ") & _
lngDiffSeconds & IIf(lngDiffSeconds <> 1, " seconds", "
second")
End If
End If

If booSwapped Then
varTemp = "-" & varTemp
End If

Diff2Dates = Trim$(varTemp)

End_Diff2Dates:
Exit Function

Err_Diff2Dates:
Resume End_Diff2Dates

End Function
'************** Code End *****************



MDI said:
Connie, I know exactly what you're trying to get. I'm having the same
problem also. I have multiple events for one filing number (ie received,
letter sent, resubmitted, sent to legal, received from legal, closed) and
each of these events has a date to it. The dates are typically more than one
day apart, so a -1 datediff doesn't help. But I know there "has" to be a way
of counting the days between each event.

Surely someone out there can give us a glimmer of hope!
HELP! This ended up NOT working. I still can't get the difference between
two (or more) ActionDates.
[quoted text clipped - 106 lines]
 
Top