我有一个带有一些绑定控件的数据表子表单,其中之一是带有 Control Source = item 的组合框 (cboItem)。项目在表设计中设置为“Required=Yes”。组合框不限于列表(可以键入值)。
当用户转到“新记录”行时,cboItem_Enter 事件为 cboItem 字段提供一个值:
Private Sub cboItem_Enter()
If Me.NewRecord Then
cboItem = 3
End If
End Sub
如果用户随后删除它(留下 Null 值),然后按 Tab 键切换到下一个字段,我希望它显示 MsgBox(“输入 Null”),然后再次为 cboItem 字段分配一个值(这就是错误发生的地方) ).
我在 Form_Error 中有这个:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
Select Case DataErr
Case 3314 'Null value entered
Response = acDataErrContinue
Case Else
Response = acDataErrDisplay
End Select
End Sub
更新前我有以下内容:
Private Sub cboItem_BeforeUpdate(Cancel As Integer)
If IsNull(cboItem) Then
MsgBox ("Null entered")
cboItem = 3 'Error occurs on this line
Cancel = True
End If
End Sub
这是错误:
我在cboItem_Exit中尝试了相同的代码。在这种情况下,不会弹出MsgBox(“输入空”),没有错误,并且未设置新值。
我也在 Form_Current / Form_BeforeUpdate / Form_AfterUpdate / cboItem_LostFocus 中尝试了相同的代码(没有 Cancel = True)。同样,没有弹出 MsgBox,没有错误,并且未设置新值。
我会使用 cboItem_BeforeUpdate 事件。它将允许您在保存到记录之前验证数据。
Private Sub cboItem_BeforeUpdate(Cancel As Integer)
If IsNull(cboItem.Value) Then
MsgBox "Null entered"
' Set a default value here
cboItem = 3
End If
End Sub