whoops.
sorry.
here is the story so far: i am working on modifying the diff2date function
to fit what i need. i have a few different places that i want to be able to
output the information created by the diff2date function. one is in a list
box where the users will see how long a product has been out of commission. i
want to be able to use a string to format the results coming from the
diff2date function.
i had been working on formatting it by using optional boolean variables in
the function, but found that the more options i wanted the more i ended up
with a pain to manage the options. i thought it would be better if i created
the ability to use one string that told the function how to lay out the
results. i am already working on this idea, but was looking for a little help
on how to get the string to parse into the yes/no's that i needed to tell the
function to lay it out properly.
here is the code that i am using currently:
Public Function Diff2Dates(Interval, strY, strM, strD, strH, strN, strS As
String, Date1 As Date, Date2 As Date, _
Optional LargestIntervalOnly, Optional
ShowPlurals, Optional ShowSpaces, Optional ShowZero, _
Optional ShowNegInterval, Optional
ShowLeadingZero, Optional ShowPeriodTime, _
Optional ShowComma As Boolean = False) As Variant
'Author: Copyright 2001 Pacific Database Pty Limited Graham R Seach MCP
MVP (e-mail address removed)
' Phone: +61 2 9872 9594 Fax: +61 2 9872 9593 This code is
freeware. Enjoy...
' (*) Amendments suggested by Douglas J. Steele MVP
' (*) Largest Interval Function, ShowSpaces, str?,
ShowNegInterval, ShowPeriodTime, ShowPlurals by Alan R Tonn
'
'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.
' the str? strings are for the kind of text the user wants to show
on the end of the amount of time for the
' interval. these are not optional but can be "".
' if the ShowPlurals is True all time outputs will show an "s" on
the end of the string the user inputs as
' the string that goes at the end of the time interval.
otherwise it shows nothing.
' if the ShowSpaces is true all time outputs will have a space
between the times and thier respective str? values
' if it is false all the numbers and interval strings will
connotate together with no space.
' if there are times that are future to your current timezone the
results will show negative if the ShowNegInterval is true.
' if the LargestIntervalOnly is true then the function only shows
the largest interval as an integer value. for example
' if the intervals allowed to show are "ymdhm" and the amount
of the interval was 0 years, 0 months, 0 days, 15 hours, 36 minutes
' then the result would be "15 hours". the largest amount will
always show, and only that interval.
' 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
'this section connects all the parts together. this is responsible for
giving the string of time intervals.
Select Case LargestIntervalOnly
Case False
If ShowPeriodTime = True Then
If lngDiffYears <> 0 Or lngDiffMonths <> 0 Or lngDiffDays <>
0 Or ShowZero = True Then
varTemp = "P"
End If
End If
If booCalcYears And (lngDiffYears > 0 Or ShowZero) Then 'P_0Y_Ys,
varTemp = varTemp & IIf(ShowSpaces = True, " ", "") &
IIf(ShowLeadingZero And lngDiffYears <= 9, "0", "") & lngDiffYears &
IIf(ShowSpaces = True, " ", "") & IIf(lngDiffYears <> 1, strY &
IIf(ShowPlurals = True, "s", " ") & IIf(ShowComma = True, Chr(44), ""), strY)
End If
If booCalcMonths And (lngDiffMonths > 0 Or ShowZero) Then
'P_0Y_Ys,_0M_Ms,
If booCalcMonths Then
varTemp = varTemp & IIf(IsNull(varTemp), Null,
IIf(ShowSpaces = True, " ", "") & IIf(ShowLeadingZero And lngDiffMonths <= 9,
"0", "") & lngDiffMonths & IIf(lngDiffMonths <> 1, IIf(ShowSpaces = True, "
", "") & strM & IIf(ShowPlurals = True, "s", " ") & IIf(ShowComma = True,
Chr(44), ""), IIf(ShowSpaces = True, " ", "") & strM))
End If
End If
If booCalcDays And (lngDiffDays > 0 Or ShowZero) Then
'P_0Y_Ys,_0M_Ms,_0D_Ds,
If booCalcDays Then
varTemp = varTemp & IIf(IsNull(varTemp), Null,
IIf(ShowSpaces = True, " ", "")) & IIf(ShowLeadingZero And lngDiffDays <= 9,
"0", "") & lngDiffDays & IIf(lngDiffDays <> 1, IIf(ShowSpaces = True, " ",
"") & strD & IIf(ShowPlurals = True, "s", " ") & IIf(ShowComma = True,
Chr(44), ""), IIf(ShowSpaces = True, " ", "") & strD)
End If
End If
If ShowPeriodTime = True Then
If (booCalcHours And lngDiffHours <> 0) Or (booCalcMinutes
And lngDiffMinutes <> 0) Or (booCalcSeconds And lngDiffSeconds <> 0) Or
ShowZero = True Then
varTemp = varTemp & " T"
End If
End If
If booCalcHours And (lngDiffHours > 0 Or ShowZero) Then
'P_0Y_Ys,_0M_Ms,_0D_Ds,
If booCalcHours Then
varTemp = varTemp & IIf(IsNull(varTemp), Null,
IIf(ShowSpaces = True, " ", "")) & IIf(ShowLeadingZero And lngDiffHours <= 9,
"0", "") & lngDiffHours & IIf(lngDiffHours <> 1, IIf(ShowSpaces = True, " ",
"") & strH & IIf(ShowPlurals = True, "s", " ") & IIf(ShowComma = True,
Chr(44), ""), IIf(ShowSpaces = True, " ", "") & strH)
End If
End If
If booCalcMinutes And (lngDiffMinutes > 0 Or ShowZero) Then
If booCalcMinutes Then
varTemp = varTemp & IIf(IsNull(varTemp), Null,
IIf(ShowSpaces = True, " ", "")) & IIf(ShowLeadingZero And lngDiffMinutes <=
9, "0", "") & lngDiffMinutes & IIf(lngDiffMinutes <> 1, IIf(ShowSpaces =
True, " ", "") & strN & IIf(ShowPlurals = True, "s", " ") & IIf(ShowComma =
True, Chr(44), ""), IIf(ShowSpaces = True, " ", "") & strN)
End If
End If
If booCalcSeconds And (lngDiffSeconds > 0 Or ShowZero) Then
If booCalcSeconds Then
varTemp = varTemp & IIf(IsNull(varTemp), Null,
IIf(ShowSpaces = True, " ", "")) & IIf(ShowLeadingZero And lngDiffSeconds <=
9, "0", "") & lngDiffSeconds & IIf(lngDiffSeconds <> 1, IIf(ShowSpaces =
True, " ", "") & strS & IIf(ShowPlurals = True, "s", " "), IIf(ShowSpaces =
True, " ", "") & strS)
End If
End If
Case True
If lngDiffYears >= 1 Then
varTemp = lngDiffYears & IIf(lngDiffYears <> 1, strY &
IIf(ShowPlurals = True, "s", ""), strY)
ElseIf lngDiffMonths >= 1 Then
varTemp = lngDiffMonths & IIf(lngDiffMonths <> 1, strM &
IIf(ShowPlurals = True, "s", ""), strM)
ElseIf lngDiffDays >= 1 Then
varTemp = lngDiffDays & IIf(lngDiffDays <> 1, strD &
IIf(ShowPlurals = True, "s", ""), strD)
ElseIf lngDiffHours >= 1 Then
varTemp = lngDiffHours & IIf(lngDiffHours <> 1, strH &
IIf(ShowPlurals = True, "s", ""), strH)
ElseIf lngDiffMinutes >= 1 Then
varTemp = lngDiffMinutes & IIf(lngDiffMinutes <> 1, strN &
IIf(ShowPlurals = True, "s", ""), strN)
Else
varTemp = lngDiffSeconds & IIf(lngDiffSeconds <> 1, strS &
IIf(ShowPlurals = True, "s", ""), strS)
End If
End Select
If ShowNegInterval = True Then
If booSwapped Then
varTemp = "-" & varTemp
End If
End If
Diff2Dates = Trim$(varTemp)
End_Diff2Dates:
Exit Function
Err_Diff2Dates:
Resume End_Diff2Dates
End Function
as you can see the if then's and booleans are a little out of hand. i am