【问题标题】:Validation rule disregards return value of VBA function验证规则忽略 VBA 函数的返回值
【发布时间】:2015-04-15 03:39:52
【问题描述】:

我有一个 VBA 函数,它根据输入的一个参数返回布尔值(真/假)。我试图将此函数插入​​ Access 表单上的验证规则中。

验证规则:

MyFunction ([My Field])

VBA 中的 MyFunction:

Public Function MyFunction(str as String) as Boolean
    MyFunction=Len(str)>4 'Just as an example, the actual function is a bit more complex, but still returns true when expected and false when expected.  This has been observed when stepping through the actual VBA code
End Function

我可以在 VBA 中看到该函数确实被调用,并且它按预期返回 true 和 false,但由于某种原因,无论函数实际返回什么,Access 仍然看到 [My Field] 的值无效(注意:[My Field] 是一个没有输入掩码的文本字段)。

那么,我必须怎么做才能让我的 VBA 函数返回的“真”值在我的验证规则中注册为有效值?

我正在使用 Access 2010。

【问题讨论】:

    标签: validation ms-access vba ms-access-2010


    【解决方案1】:

    为其他遇到此问题的人想办法。与 Access 如何存储真假有关。

    所以,使用

    MyFunction([My Field]) = -1
    

    验证规则为我解决了这个问题。

    【讨论】:

    • 我怀疑MyFunction([My Field]) = True 也可以。由于您定义了函数... As Boolean,因此确实没有必要这样做,但我已经看到SQL语句在... WHERE someBooleanValue中的行为不正确并且在... WHERE someBooleanValue = True中正常工作的情况。
    • 是的。 MS Access 是一个奇怪的产品。如果我可以选择桌面应用程序,我宁愿使用 WPF 和 .Net,其中布尔值的行为类似于布尔值,并且您可以将业务逻辑与视图分开。
    猜你喜欢
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多