Vba MS访问奇怪的数字

Vba MS访问奇怪的数字,vba,forms,ms-access,double,Vba,Forms,Ms Access,Double,我从VBA中的一个函数收到奇怪的数字 我有一个连续的表单,其中有一个按钮,用户可以从中操纵文本框中的小时总数。此文本框位于表单页脚中 我的代码是这样的: Private Sub Option39_Click() Dim time As Double 'calculate time to format time = 25 / 24 If Option39.Value = True Then Debug.Print dblTotal

我从VBA中的一个函数收到奇怪的数字

我有一个连续的表单,其中有一个按钮,用户可以从中操纵文本框中的小时总数。此文本框位于表单页脚中

我的代码是这样的:

Private Sub Option39_Click()
    Dim time As Double

    'calculate time to format
    time = 25 / 24

    If Option39.Value = True Then
         Debug.Print dblTotal
        dblTotal = dblTotal + time
         Debug.Print dblTotal
        Me.txtTotalTeamTotal = FormatUnlimitedHours(dblTotal)
         Debug.Print dblTotal
    Else
        dblTotal = dblTotal - time
        Me.txtTotalTeamTotal = FormatUnlimitedHours(dblTotal)
    End If
End Sub
Public Function FormatUnlimitedHours(time As Variant) As Variant
    'function that can have unlimited number of hours in hh:mm:ss format
    Dim comma As Integer
    Dim hours As Variant
    Dim minutes As Variant
    'switch to hours format
    time = time * 24

    If time > 23 Then

        comma = InStr(time, ",") - 1

        If Not comma < 0 Then
            minutes = "0," & Mid(time, comma + 2, Len(time) - comma + 1)
            minutes = format(minutes / 24, "hh:mm:ss")
            hours = CDbl(Left(time, comma)) + CDbl(Left(minutes, InStr(minutes, ":") - 1))
            FormatUnlimitedHours = hours & ":" & Mid(minutes, InStr(minutes, ":") + 1, 5)
            Exit Function
        Else
            'for whole numbers
            FormatUnlimitedHours = time & ":00:00"
            Exit Function
        End If

    End If

    FormatUnlimitedHours = format(time / 24, "hh:mm:ss")

End Function
我从debug.print接收这些值

3,66611111111111 
4,70777777777778 
112,986666666667 
我不明白为什么
dblTotal
将其值从
470777778更改为1129866666667
为什么更改了数字

FormatUnlimitedHours()函数的定义如下:

Private Sub Option39_Click()
    Dim time As Double

    'calculate time to format
    time = 25 / 24

    If Option39.Value = True Then
         Debug.Print dblTotal
        dblTotal = dblTotal + time
         Debug.Print dblTotal
        Me.txtTotalTeamTotal = FormatUnlimitedHours(dblTotal)
         Debug.Print dblTotal
    Else
        dblTotal = dblTotal - time
        Me.txtTotalTeamTotal = FormatUnlimitedHours(dblTotal)
    End If
End Sub
Public Function FormatUnlimitedHours(time As Variant) As Variant
    'function that can have unlimited number of hours in hh:mm:ss format
    Dim comma As Integer
    Dim hours As Variant
    Dim minutes As Variant
    'switch to hours format
    time = time * 24

    If time > 23 Then

        comma = InStr(time, ",") - 1

        If Not comma < 0 Then
            minutes = "0," & Mid(time, comma + 2, Len(time) - comma + 1)
            minutes = format(minutes / 24, "hh:mm:ss")
            hours = CDbl(Left(time, comma)) + CDbl(Left(minutes, InStr(minutes, ":") - 1))
            FormatUnlimitedHours = hours & ":" & Mid(minutes, InStr(minutes, ":") + 1, 5)
            Exit Function
        Else
            'for whole numbers
            FormatUnlimitedHours = time & ":00:00"
            Exit Function
        End If

    End If

    FormatUnlimitedHours = format(time / 24, "hh:mm:ss")

End Function
蒂姆·威廉姆斯已经回答了你的问题。但是,您不应该将日期和时间当作DateTime以外的任何东西来处理。这只会使事情复杂化

例如,在大多数英语国家,逗号不是小数分隔符,DateTime的“base”类型是Double,因此通常在DateTime和Double之间来回转换没有区别

下面是一个遵循这些规则的类似函数的示例-这也使它变得简单得多:

公共函数格式hourminutesecond(_
ByVal datTime作为日期_
可选的ByVal stresparator作为字符串=“:”)_
作为字符串
'返回datTime的天、小时、分钟和秒数
'转换为小时、分钟和秒作为格式化字符串
'带有可选的时间分隔符选择。
'
例如:
“datTime:#10:03:55#+#20:01:24#
'返回时间:30:05:19
'
' 2014-06-17. 仙人掌数据ApS,CPH。
暗弦
作为字符串的Dim strMinuteSec
暗弦
strHour=CStr(固定(基准时间)*24+小时(基准时间))
'必要时将前导零添加到分钟和秒计数。
strMinuteSec=Right(“0”和CStr(分钟(数据时间)),2)和STRESPARATOR&Right(“0”和CStr(秒(数据时间)),2)
strHours=strHour&STRESPARATOR&strMinuteSec
FormatHourMinuteSecond=strHours
端函数
例如:

?格式小时数秒(25/24)
25:00:00

默认情况下,在VBA中,参数是通过引用传递的,这意味着当您将
时间
乘以24时,您也会影响调用子函数中
dblTotal
的值。@Tim Williams指出的一点是,通过val传递参数会创建一个新的、单独的变量,以便在给定的函数中使用<代码>函数格式UnlimitedHours(ByVal time作为变量)将创建一个名为
time
的变量。如果假定ByRef未指定传输方法,则不会创建副本,函数可以更改值,并且新值将可用于调用过程。这是一个非常有用的功能。非常感谢:)