Summing up Hours and Minutes in MS Access - How to deal with huge datasets

荒凉一梦 提交于 2021-02-17 05:27:47

问题


So I have a Table in MS Access 2010 which looks like this:

Date           Pers. ID      Dauer Tätigkeit

10.04.2016     10            01:15
11.05.2016     5             05:00
...

I want to get the total hours of "Dauer Tätigkeit". All entries in the "Dauer Tätigkeit" are in the Date Format.

The formula below is supposed to do the job for listing the end in a Format like '1346:30'. It actually does the job for small datasets.

The dataset I have to work with though has around 800 entries with an hourly average of 3 hours. So i am expecting to get a number in the ballpark of around 2400 to 8000 hours, if i fill every line with 23:59 it should at the max give me a number under 20.000. I get a number well south of 1 million.

The error dissapears when the total of Hours is smaller than roughly 500 (I tested this by halfing the number of entries twice, not further). So I think, I am having an overflow problem somewhere.

Question now is where. The obvious point where this could happen is the "Int" just at the beginning, but removing it from the formula doesn't solve it.

 =Format(Int(Summe([Dauer Tätigkeit]))*24+Stunde(Summe([Dauer Tätigkeit]));"00") 
     & ":" & Format(Minute(Summe([Dauer Tätigkeit]));"00")

My questions now are: Is there a syntax problem? Is it an overflow problem? Can a cast to LongInt solve it? If yes, how to implement it into the formula? Am I asking the right questions?


回答1:


Always handle dates as dates, not strings, not long, neither currency.

This function will do:

Public Function FormatHourMinute( _
  ByVal datTime As Date, _
  Optional ByVal strSeparator As String = ":") _
  As String

' Returns count of days, hours and minutes of datTime
' converted to hours and minutes as a formatted string
' with an optional choice of time separator.
'
' Example:
'   datTime: #10:03# + #20:01#
'   returns: 30:04
'
' 2005-02-05. Cactus Data ApS, CPH.

  Dim strHour       As String
  Dim strMinute     As String
  Dim strHourMinute As String

  strHour = CStr(Fix(datTime) * 24 + Hour(datTime))
  ' Add leading zero to minute count when needed.
  strMinute = Right("0" & CStr(Minute(datTime)), 2)
  strHourMinute = strHour & strSeparator & strMinute

  FormatHourMinute = strHourMinute

End Function

For your example, the output will be:

Result = FormatHourMinute(#23.59# * 800)
' Result -> 19186:40

And your expression will be:

=FormatHourMinute(Summe([Dauer Tätigkeit]))


来源:https://stackoverflow.com/questions/39214969/summing-up-hours-and-minutes-in-ms-access-how-to-deal-with-huge-datasets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!