Office中国论坛/Access中国论坛

标题: [求助]有关日期/时间的计算 [打印本页]

作者: ffish    时间: 2007-4-29 05:55
标题: [求助]有关日期/时间的计算
本人是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”或“ ”,同时结束函数。

先谢谢各位了。
作者: 一点通    时间: 2007-4-29 06:38
语句太长,一时难以分析,有关时间的运算可参考一下这个贴

http://www.office-cn.net/forum.php?mod=viewthread&tid=33813&replyID=&skin=1
作者: ffish    时间: 2007-4-29 07:15
谢谢管理员及时回复。

由于上面的例子中涉及时间(小时、分钟)的计算,所以单纯日期的计算不能满足我的要求,所以在access的帮助里翻到这个函数使用。

上面说了那么多,是表明我的表想做什么而已,发现自己太罗嗦了。

实际上我想问的是:怎么在上面的函数加入一个判断语句,在(dateTimeStart)为空时,直接跳过函数过程,强行为ElapsedTimeString函数赋值为“0”或“ ”,并关闭函数,从而避免返回“# 错误”的结果。
作者: ffish    时间: 2007-4-29 07:19
或者换个短点的函数,是计算两日期间相差多少天的。

-------------------------------------------------------------


Public Function ElapsedDays(dateTimeStart As Date, dateTimeEnd As Date) As String
'*********************************************************************
' Function ElapsedDays(dateTimeStart As Date, dateTimeEnd As Date) As String
' Returns the time elapsed in days between a starting Date/Time and
' an ending Date/Time formatted as a string that looks like this:
' "10 天" or "1 天".
'*********************************************************************
Dim interval As Double, days As Variant
If IsNull(dateTimeStart) = True Or _
   IsNull(dateTimeEnd) = True Then Exit Function
interval = dateTimeEnd - dateTimeStart
days = Fix(CSng(interval))
ElapsedDays = IIf(days = 1, days & "   ", days & "   ")
End Function

----------------------------------------------------------------------------

问题:怎么在上面的函数中加入一个判断语句,在(dateTimeStart)为空时,直接跳过函数过程,强行为ElapsedDays函数赋值为“0”或“ ”,并关闭函数,从而避免返回“# 错误”的结果。
作者: eyewitnes    时间: 2007-4-29 20:23
调用ElapsedDays函数的地方允许0值或者空值不?
作者: ffish    时间: 2007-4-30 03:49
默认是留空,允许0值或者空值的,A或B或C可以是空,因为没有发生事件。
作者: 一点通    时间: 2007-4-30 04:46
上传例子
作者: andymark    时间: 2007-4-30 09:18
各赋予一个默认值
作者: ffish    时间: 2007-4-30 20:22
以下是引用一点通在2007-4-29 20:46:00的发言:
上传例子

上面的函数都是从ACCESS ONLINE HELP 中的示例数据库中的模块提取的:

http://office.microsoft.com/zh-cn/access/HA011102182052.aspx?pid=CL100570042052

为自己实际需要,对单位和显示方式做了些修改.

我的数据库(相应部分截取,改了名称,数据属性,说明均无改变:
[attach]24236[/attach]


[此贴子已经被作者于2007-4-30 12:33:01编辑过]


作者: ffish    时间: 2007-4-30 20:29
以下是引用andymark在2007-4-30 1:18:00的发言:


各赋予一个默认值

A、B、C分别默认为“0”时,表中显示是“1899年xx月xx日 xx:xx”,但如果需要对A与X间进行时间比较时,就会出现问题:(

数据库见上贴。
作者: 一点通    时间: 2007-4-30 21:40
在结果中再作判断可以吗?

[attach]24237[/attach]

作者: ffish    时间: 2007-5-1 04:13
以下是引用一点通在2007-4-30 13:40:00的发言:


在结果中再作判断可以吗?

[attach]24237[/attach]



... ...

我是猪!:(

多谢管理员,虽然要每个事件都重写一次,有点没有效率,但总算是解决了。

这方法其实也试过,不过自己很傻瓜的在ElapsedDays前加了个“=”,结果每次都显示“错误”,以为行不通,从没有想过是自己公式写错的说,晕死...

谢谢!!!
作者: 7777777    时间: 2009-2-5 10:39
dddddddd
作者: shanen    时间: 2009-2-24 13:01
学习了,谢谢




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3