Access 中的组合框比较不稳定 VBA
Erratic combobox comparison in Access VBA
我已将表单上两个不同的未绑定组合框 cbo_MaxCost 和 cbo_MinCost 的行源设置为相同的 table。这些组合框旨在为另一个 table 中的每条记录指定值范围的最小值和最大值。为了确保他们这样做,我在每个组合框的 AfterUpdate 事件中设置了代码来检查
cbo_MaxCost >= cbo_MinCost
或警告用户错误。
问题是,此检查执行不稳定。例如,有时 Access VBA 会计算
21 > 11
为假,其他时候为真。使用点和感叹号符号没有区别,使用与否 "Me." 或“.Value”与否也没有区别。单步执行它表明 Access 在比较期间从组合框中注册了正确的值。当我 select 的值比以前在其中一个框中的值高得多或低得多时,这种行为似乎更常发生。我不知道是什么原因造成的。
这是代码:
Private Sub cbo_MaxCost_AfterUpdate()
If cbo_MaxCost >= cbo_MinCost Then
MsgBox ("Access is calculating correctly.")
Else
MsgBox ("The maximum cost should be higher than the minimum.")
End If
End Sub
Private Sub cbo_MinCost_AfterUpdate()
If cbo_MaxCost >= cbo_MinCost Then
MsgBox ("Access is calculating correctly.")
Else
MsgBox ("The maximum cost should be higher than the minimum.")
End If
End Sub
请记住,combobox/listbox 总是 returns text,因此在比较之前转换为数字:
CCur(Me!cbo_MaxCost.Value) >= CCur(Me!cbo_MinCost.Value)
我已将表单上两个不同的未绑定组合框 cbo_MaxCost 和 cbo_MinCost 的行源设置为相同的 table。这些组合框旨在为另一个 table 中的每条记录指定值范围的最小值和最大值。为了确保他们这样做,我在每个组合框的 AfterUpdate 事件中设置了代码来检查
cbo_MaxCost >= cbo_MinCost
或警告用户错误。
问题是,此检查执行不稳定。例如,有时 Access VBA 会计算
21 > 11
为假,其他时候为真。使用点和感叹号符号没有区别,使用与否 "Me." 或“.Value”与否也没有区别。单步执行它表明 Access 在比较期间从组合框中注册了正确的值。当我 select 的值比以前在其中一个框中的值高得多或低得多时,这种行为似乎更常发生。我不知道是什么原因造成的。
这是代码:
Private Sub cbo_MaxCost_AfterUpdate()
If cbo_MaxCost >= cbo_MinCost Then
MsgBox ("Access is calculating correctly.")
Else
MsgBox ("The maximum cost should be higher than the minimum.")
End If
End Sub
Private Sub cbo_MinCost_AfterUpdate()
If cbo_MaxCost >= cbo_MinCost Then
MsgBox ("Access is calculating correctly.")
Else
MsgBox ("The maximum cost should be higher than the minimum.")
End If
End Sub
请记住,combobox/listbox 总是 returns text,因此在比较之前转换为数字:
CCur(Me!cbo_MaxCost.Value) >= CCur(Me!cbo_MinCost.Value)