|
本人是access的菜鸟,有个关于日期计算的问题想求教各位。
有这样三个事件A、B、C,它们可以相继发生,有些可能不会发生,但不可能同时出现,而报表中要求同时计算三者出现的持续时间,如果没有发生,则报表显示“0”或留空。
我在表中这样设置了字段:
X发生时间(与A、B、C无关);A发生时间;A结束时间;B发生时间;B结束时间;C发生时间;C结束时间;
以上字段均是“日期/时间”属性;格式是:yyyy\年mm\月dd"日 "hh:nn ;掩码是:0000"年"00"月"00"日 "00":"00;0;0 ;默认值留空。
报表是这样的:
X=
A持续时间=
B持续时间=
C持续时间=
日期计算采用了ACCESS日期计算示例中的ElapsedTimeString函数
-----------------------------------------------------------------------------------------
Public Function ElapsedTimeString(dateTimeStart As Date, dateTimeEnd As Date) As String
'*********************************************************************
' Function ElapsedTimeString(dateTimeStart As Date, dateTimeEnd As Date) As String
' Returns the time elapsed between a starting Date/Time and an ending
' Date/Time formatted as a string that looks like this:
' "10 天, 20 小时, 30 分钟, 40 秒".
'*********************************************************************
Dim interval As Double, str As String, days As Variant
Dim hours As String, minutes As String, seconds As String
If IsNull(dateTimeStart) = True Or _
IsNull(dateTimeEnd) = True Then Exit Function
interval = dateTimeEnd - dateTimeStart
days = Fix(CSng(interval))
hours = Format(interval, "h")
minutes = Format(interval, "n")
seconds = Format(interval, "s")
' Days part of the string
str = IIf(days = 0, "", _
IIf(days = 1, days & "天", days & "天"))
str = str & IIf(days = 0, "", _
IIf(hours & minutes & seconds <> "000", ", ", " "))
' Hours part of the string
str = str & IIf(hours = "0", "", _
IIf(hours = "1", hours & "小时", hours & "小时"))
str = str & IIf(hours = "0", "", _
IIf(minutes & seconds <> "00", ", ", " "))
' Minutes part of the string
str = str & IIf(minutes = "0", "", _
IIf(minutes = "1", minutes & "分钟", minutes & "分钟"))
str = str & IIf(minutes = "0", "", IIf(seconds <> "0", ", ", " "))
' Seconds part of the string
str = str & IIf(seconds = "0", "", _
IIf(seconds = "1", seconds & "秒", seconds & "秒"))
ElapsedTimeString = IIf(str = "", "0", str)
End Function
----------------------------------------------------------------------------------------
问题是如果A、B、C中有某个的发生时间和结束时间为空(也就是没有发生)的话,相应报表计算的结果就是“#错误”。
我想问的是,在不改动表的前提下,对这个函数怎样修改,可以做到在发生和/或结束时间为空时,跳过运算过程而直接给ElapsedTimeString赋值为“0”或“ ”,同时结束函数。
先谢谢各位了。 |
|