vba BeforeUpdate 问题 - 运行时错误 2115

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1553833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:52:31  来源:igfitidea点击:

BeforeUpdate issue - Runtime error 2115

ms-accessvbaaccess-vba

提问by SmartestVEGA

I have written the following query:

我编写了以下查询:

Private Sub Size_Sqft_BeforeUpdate(Cancel As Integer)
  Me!Size_Sqft = Nz(Me!Size_Sqft, 0)
End Sub

But while removing the zero in the field to make it null, I am getting the following error:

但是在删除字段中的零以使其为空时,我收到以下错误:

Runtime error 2115

Macro and function set to before update and validation rule property for this field is preventing manual data entry screen for company from saving the data in the field.

运行时错误 2115

将此字段的更新和验证规则属性设置为之前的宏和函数阻止公司的手动数据输入屏幕将数据保存在该字段中。

采纳答案by Tony Toews

You have to put that code in the AfterUpdate event of that field.

您必须将该代码放在该字段的 AfterUpdate 事件中。

回答by RubberDuck

I know this is an old thread, and has already been answered, but there is another solution that doesn't require several writes back to your database. I'm adding it in case someone else comes across this question.

我知道这是一个旧线程,并且已经得到回答,但是还有另一种解决方案不需要多次写回您的数据库。我添加它以防其他人遇到这个问题。

Private Sub ControlName_BeforeUpdate(Cancel as integer)
    If isValid(Me.ControlName.Value) = False Then
        Cancel = True
        Me.ControlName.Undo
    End If
End Sub

Private Function isValid(ByVal...) as boolean
    ' validate control value here
End Function