【发布时间】:2018-07-10 16:53:21
【问题描述】:
这里有点菜鸟问题。您如何使用 Access Events 处理数据验证?问题是,当我使用 SetFocus 将光标返回到包含错误数据的字段时,Access 会通过 Tab Order 中下一个控件的 _Exit 和/或 _LostFocus 事件。如果这些包括数据验证过程,则绕过所需的 SetFocus。
使用techturtles answer,我想出了这个“解决方案”(阅读“hack”)。
Private Sub ServicingEmployee_LostFocus() 'Check data validity
If dataEntryCancelled Then Exit Sub
Dim cntrl As Control
Set cntrl = Me.ServicingEmployee
Dim myResponse As Integer
If IsNull(cntrl.Value) Then
myResponse = MsgBox("This field cannot be left blank.", vbOKOnly, "Enter Date Collected")
dataErrorInPreviousField = True
**'Next line sets a Public Control**
Set controlWithDataEntryError = Me.ServicingEmployee
End If
End Sub
Private Sub Client_GotFocus() '**Check for data Error in previous Control according to Tab Order**
If dataErrorInPreviousField Then Call dataErrorProc
End Sub
Public Sub dataErrorProc()
With controlWithDataEntryError
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
dataErrorInPreviousField = False
End Sub
Private Sub Client_Exit(Cancel As Integer) '**Example of Bypassing _Exit Event**
If dataEntryCancelled Or dataErrorInPreviousField Then Exit Sub
.
...
End Sub
我的问题是:有没有更简单的方法可以做到这一点?
【问题讨论】:
-
考虑控件的 BeforeUupdate 事件。而不是 LostFocus。
-
我同意@HansUp - 控件的
BeforeUpdate更新最适合单个字段验证检查。表单的BeforeUpdate事件非常适合检查整个表格中必填字段中的空白条目 -
@dbmitch,你之前至少回答了我的一个问题,所以我有一个相关的问题。你熟悉 VB.net 吗?过渡到 VB.net 值得学习曲线吗?我熟悉 Objective C。
-
我根本不会在 VB.Net 中编码
标签: validation ms-access vba