Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > Access数据库-模块/函数/VBA

介绍几个计算时间间隔的自定义函数

时 间:2012-04-19 21:45:40
作 者:http://office.microsoft.com/   ID:12122  城市:江门
摘 要:介绍几个计算时间间隔的自定义函数
正 文:

         点击下载此附件

    计算经过的时间非常简单。Access 可以计算两个日期之间的时间间隔,甚至相隔数月或数年。不过,以所需方式显示结果值就要求一定的技巧和VBA知识。   

      下面显示的 HoursAndMinutes 函数是将时间间隔设置为“小时:分钟”格式的自定义函数。它用来正确处理计算的时间结果,例如每周时间表报告可能用到的时间总和。您向该函数传递一个时间间隔或通过它进行“日期/时间”计算(例如 #12/24/2003 11:00PM# - #12/10/2003 6:00AM#),它就会返回正确格式的字符串(在本例中为 353:00)。若要指定通常形式的“日期/时间”字符串,请将日期和时间部分包括在 # 号内。为了明确,您应在提供文字字符串时尽量同时指定日期和时间部分,以避免错误。

Public Function HoursAndMinutes(interval As Variant) As String
'***********************************************************************
' Function HoursAndMinutes(interval As Variant) As String
' Returns time interval formatted as a hours:minutes string
'***********************************************************************
Dim totalminutes As Long, totalseconds As Long
Dim hours As Long, minutes As Long, seconds As Long
If IsNull(interval) = True Then Exit Function

hours = Int(CSng(interval * 24))

totalminutes = Int(CSng(interval * 1440))   ' 1440 = 24 hrs * 60 mins
minutes = totalminutes Mod 60

totalseconds = Int(CSng(interval * 86400))  ' 86400 = 1440 * 60 secs
seconds = totalseconds Mod 60

If seconds > 30 Then minutes = minutes + 1  ' round up the minutes and
If minutes > 59 Then hours = hours + 1: minutes = 0 ' adjust hours

HoursAndMinutes = hours & ":" & Format(minutes, "00")
End Function

HoursAndMinutes 函数通过分别计算小时、分钟、秒部分得出时间间隔的各个部分。从一个“日期/时间”值减去另一个“日期/时间”值时,时间间隔结果是一个双精度数值,小数点左边是天数,右边是剩余时间相对于一天的时间比例。若要得到小时部分结果,请将该部分值乘以 24。分钟部分值则是先将间隔乘以 1,440(一天的分钟数),得出该部分间隔值表示的总分钟数。然后,将结果数除以 60 以去掉在小时结果中已包括的分钟数,得到的余数就是需要的分钟数值(Mod 运算符用于进行此操作)。与此类似,先计算总秒数再除以一天的总秒数,得出的余数就是秒数值。

其余函数部分用于将结果取整,得出小时和分钟结果,组成结果字符串。

有时,您希望结果更为直白、易懂。下面列出了另一个函数,在输入的两个“日期/时间”值后,可返回形式如“10 days, 20 hours, 30 minutes, 40 seconds”的长字符串。

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 days, 20 hours, 30 minutes, 40 seconds".
'**********************************************************************************************
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 & " Day", 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 & " Hour", 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 & " Minute", minutes & " Minutes"))
str = str & IIf(minutes = "0", "", IIf(seconds <> "0", ", ", " "))
' Seconds part of the string
str = str & IIf(seconds = "0", "", _
   IIf(seconds = "1", seconds & " Second", seconds & " Seconds"))
ElapsedTimeString = IIf(str = "", "0", str)
End Function

ElapsedTimeString 函数通过先从结束时间减去开始时间得出时间间隔,再通过使用 Format 函数分别确定天、小时、分钟、秒部分。然后,函数按所需形式组成结果字符串,并考虑单数结果的情形(使用“Day”代替“Days”),去掉不必要的部分(如“0 seconds”)。

    如果仅关注天数,您可以使用下面的 ElapsedDays 函数,该函数删除了 ElapsedTimeString 函数中一些不必要的代码。

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 days" or "1 day".
'****************************************************************************************
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 & " Day", days & " Days")
End Function

您也可能希望在窗体或报表中显示经过的时间。您可以先在窗体或报表的设计界面中添加文本框控件。控件是显示数据、执行操作或装饰窗体或报表的一种对象。文本框控件是一种可在其中输入或显示文本(如计算结果)的控件。

为了向报表中添加 TimeToShip 计算,您还需要向文本框的“控件来源”属性中添加表达式。属性是与控件相关联的特性,Access 已预先装载这些特性。“控件来源”属性表示 Access 从何处获取控件数据。然后,在文本框的“控件来源”属性中插入以下表达式即可在该文本框中显示经过的时间。

=ElapsedTimeString([OrderDate],[ShippedDate])

请特别注意表达式开头处要有等号 (=)。设置“控件来源”属性时,需要使用等号向 Access 表示它是一个表达式,而不是字段名称。

或者,假设您希望创建时间表窗体来跟踪您的所有雇员的工作小时数和分钟数,您可以创建一个包含 TimeIn 字段和 TimeOut 字段的窗体。然后,只需计算一天中工作的小时数和分钟数即可。TimeandMinutes 函数可以完成这一工作。请先创建一个 HoursWorked 文本框,然后在文本框的控件来源属性中插入以下表达式。

=HoursAndMinutes([TimeOut]-[TimeIn])


Access软件网QQ交流群 (群号:483923997)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助