Access 中的总时间字段

问题描述 投票:0回答:1

我在 MS access 中创建了三个表来跟踪骑行/装备使用情况,第一个表具有活动 ID 和骑行持续时间 (hh:mm:ss),第二个表具有活动 ID 和装备 ID,第三个表具有装备 ID 和名称对于齿轮,所有三个表都使用关系连接。

我希望以小时为单位跟踪总齿轮使用情况,例如:Gear XX : 135h:45mm:59ss,但访问将持续时间字段视为日期和时间(24 小时格式),总和结果看起来像时间格式(24 小时) )。持续时间字段在表中创建为日期/时间,是否可以在访问中对时间求和?已尝试将格式更改为 hh:nn:ss,但似乎不起作用。所有表格都是使用 Excel 工作表导入创建的,我没有开始或结束时间来使用 datediff 之类的东西。

ms-access
1个回答
0
投票

您需要一个自定义函数来格式化时间总和,例如:

' Format the count of days, hours, minutes, and seconds of Date1 as
' hours, minutes, and seconds.
' By default, the local time separator is used.
' Optionally, specify a custom time separator.
'
' Example:
'   Date1:      #10:03:01# + #20:01:07#
'   returns:    30:04:08
'
' 2024-11-30. Cactus Data ApS, CPH.
'
Public Function FormatHourMinuteSecond( _
    ByVal Date1 As Date, _
    Optional ByVal Separator As String) _
    As String

    Dim TextHour                As String
    Dim TextMinute              As String
    Dim TextSecond              As String
    Dim TextHourMinuteSecond    As String
    
    TextHour = CStr(Fix(Date1) * 24 + Hour(Date1))
    ' Maintain a leading zero for the minute count.
    TextMinute = Right("0" & CStr(Minute(Date1)), 2)
    ' Maintain a leading zero for the second count.
    TextSecond = Right("0" & CStr(Second(Date1)), 2)
    
    If Separator = "" Then
        Separator = FormatSystemTimeSeparator
    End If
    TextHourMinuteSecond = TextHour & Separator & TextMinute & Separator & TextSecond
    
    FormatHourMinuteSecond = TextHourMinuteSecond
  
End Function


' Obtain the system time separator without API calls.
'
' 2021-01-26. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function FormatSystemTimeSeparator() As String

    Dim Separator   As String
    
    Separator = Format(Time, ":")

    FormatSystemTimeSeparator = Separator
    
End Function

尚未添加到我在 GitHub 上的存储库:VBA.Date

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.