Validation 是否有内置的MS Access VBA事件来简化数据验证和设置焦点

Validation 是否有内置的MS Access VBA事件来简化数据验证和设置焦点,validation,ms-access,vba,Validation,Ms Access,Vba,这是个新手问题。如何使用访问事件处理数据验证?问题是,当我使用SetFocus将光标返回到包含错误数据的字段时,Access会按照Tab顺序通过下一个控件的_Exit和/或_LostFocus事件。如果这些包括数据验证程序,则会绕过所需的SetFocus 使用,我想出了这个“解决方案”(读“黑客”) 我的问题是:有没有更简单的方法来实现这一点?首先,我强烈建议您在表单验证之外添加表约束,如果您还没有这样做的话。假设此表单绑定到Access中的表,只需确保表中的ServicingEmployee字

这是个新手问题。如何使用访问事件处理数据验证?问题是,当我使用SetFocus将光标返回到包含错误数据的字段时,Access会按照Tab顺序通过下一个控件的_Exit和/或_LostFocus事件。如果这些包括数据验证程序,则会绕过所需的SetFocus

使用,我想出了这个“解决方案”(读“黑客”)


我的问题是:有没有更简单的方法来实现这一点?

首先,我强烈建议您在表单验证之外添加表约束,如果您还没有这样做的话。假设此表单绑定到Access中的表,只需确保表中的
ServicingEmployee
字段设置为required。这样,您就有了另一个验证级别,以确保字段具有值

对于表单验证,可以使用表单或控件的
BeforeUpdate
事件。如果使用表单的事件,则只需运行一次检查

我还建议(在表单中)有一个单独的功能来检查有效性。类似这样的内容(在这里,我使用一个绑定到Employees表的表单,其名字和姓氏字段都应为非空):


考虑控件的BeFueUpdate事件,而不是LoSoCopy。我同意@ HANSUP-控件的代码> BeFurUpdate 更新对于单个字段验证检查是最好的。表单的
BeforeUpdate
事件对于检查整个表中的必填项非常有用fields@dbmitch,您以前至少回答过我的一个问题,因此我有一个相关问题。你熟悉VB.net吗?向VB.net的过渡值得学习吗?我熟悉Objective C。我根本不使用VB.Net编写代码
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    
Private Function isFormValid() As Boolean
   ' we start off assuming form is valid
   isFormValid = True

   If IsNull(Me.txtFirstName) Then
      MsgBox "First name cannot be blank", vbOKOnly
      Me.txtFirstName.SetFocus
      isFormValid = False
   ElseIf IsNull(Me.txtLastName) Then
      MsgBox "Last name cannot be blank", vbOKOnly
      Me.txtLastName.SetFocus
      isFormValid = False
   End If
End Function

Private Sub Form_BeforeUpdate(Cancel As Integer)
   If Not isFormValid Then
      Cancel = 1
   End If
End Sub