我正在使用MS Access 2007和VBA。我有一段代码,当数量大于库存或库存为零时触发。
Private Sub Quantity_BeforeUpdate(Cancel As Integer)
If Me.Quantity > Me.Stock Or Me.Stock = 0 Then
MsgBox "Not enough stocks left."
Me.Quantity = ""
End If
End Sub
出现消息框,但随后出现错误消息:
Run-time error '2147352567 (80020009)':
The macro or function set to BeforeUpdate or ValidationRule property for this field is preventing Microsoft Office Access from saving the data in the field.
如何停止此错误?
控件的更新前事件不允许您将其更改为任何值。此时,您有2个选择:
Cancel
,然后调用Undo
将其值恢复为尝试更新之前的值Cancel
仅调用,将新值(不可接受的值)保留在适当的位置,但在继续操作之前迫使用户将其替换为可接受的值这是第一个选项的经过测试的示例代码。如果您喜欢第二种选择,则放弃该Me.Quantity.Undo
行。
Private Sub Quantity_BeforeUpdate(Cancel As Integer)
If Me.Quantity > Me.Stock Or Me.Stock = 0 Then
MsgBox "Not enough stocks left."
'Me.Quantity = "" ' <-- NOT ALLOWED
Cancel = True
Me.Quantity.Undo
End If
End Sub
当Me.Quantity
和Me.Stock
是包含数字的文本值时,情况更加复杂。如果是我,我希望将其数据类型更改为数字。但是,如果这种更改不切实际,则可以使用Val()
将文本值转换为实际数字进行比较。
Private Sub Quantity_BeforeUpdate(Cancel As Integer)
If Val(Me.Quantity) > Val(Me.Stock) Or Val(Me.Stock) = 0 Then
MsgBox "Not enough stocks left."
'Me.Quantity = "" ' <-- NOT ALLOWED
Cancel = True
Me.Quantity.Undo
End If
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句