首页 > 解决方案 > MS Access VBA布尔值本地化问题

问题描述

我在 MS Access 数据库(当前 Office 365 Access 版本)中的 VBA 中构建了一些 SQL 查询字符串,我遇到了从表单控件读取的布尔值的奇怪本地化问题Me.MyBooleanControl返回“Wahr”(德语“True”)=> 生成SQL 失败。

我现在正在使用一种解决方法,通过一个函数包装每个值请求,以将值转换为-1/0就像这里建议的那样,但我真的很想知道是否有直接的方法来避免这种情况。去年我在另一个没有遇到相同问题的 Access 数据库中使用了相同的代码结构 - 我完全不知道有什么不同(除了 MS Office 我已经更新)。

编辑:我找到了解决方案,请参阅下面的答案。

标签: ms-access

解决方案


Trying to reproduce the problem independently from the rest of my code, I nailed it down and found it to be my fault: I initialized the unbound checkbox as True which seemed to work fine, but causes the problem:

Private Sub Form_Load()
    Me.UnboundCheckbox01 = True
    Me.UnboundCheckbox02 = -1
End Sub

Private Sub ReadCheckBox()
    Debug.Print Me.UnboundCheckbox01 ' returns localized value "Wahr"
    Debug.Print Me.UnboundCheckbox02 ' returns -1 as expected

    ' after manually toggeling UnboundCheckbox01 to unchecked
    ' and back to checked again, the value returns as -1 too
End Sub

Sorry! Next time I'll honor the rules and provide some code - sometimes the question dissolves by just doing that...


推荐阅读