首页 > 解决方案 > MsAccess 表单 - 最初为空记录,但需要在数据输入开始后强制填写

问题描述

我有 MsAccess 表格:

ClientName, ID fields - records filled with the information
Gender, Race, PrimaryLanguage fields (combo box type) are empty records

用户将使用这些信息填写 Gender、Race 和 PrimaryLanguage、PrimaryDisability 字段。我的目标是 - 在用户移动到下一条记录之前,我希望他在这些字段(性别/种族)填满其内容之前无法继续。

问题是 - Gender / Race / PrimaryLanguage,现有客户的PrimaryDisability必须保持空白,但同时 - 当用户开始填写时,需要强制填写。

只有在我添加新记录的情况下,它才会以这种方式工作。(我有需要的性别/种族字段) - 但不适用于现有记录。

PS - 下面提供的答案很棒,但我只为 2 个字段工作了 3 个或更多字段呢?

我想将事件过程添加到表单中的每个按钮,而不是整个表单。代码应该是这样的:

Private Sub Gender_BeforeUpdate(Cancel As Integer)
    If Nz(Me.Gender, "") = "" And (Nz(Me.Race, "") <> "" 
       Or (Nz(Me. PrimaryLanguage, "") <> ""  
       Or (Nz(Me. PrimaryDisability, "") <> "")
    Then
       MsgBox "Select Gender", vbInformation
       Cancel = True
   End If
End Sub

但它说的是语法错误

请帮忙!

标签: formsms-accesscomboboxrequired-field

解决方案


BeforeUpdate在移动到另一条记录之前使用事件取消记录的更新

 Private Sub Form_BeforeUpdate(Cancel As Integer)


  If Nz(Me.ClientName, "") <> "" And Nz(Me.Gender, "") = "" And Nz(Me.Race, "") = "" And Nz(Me.PrimaryLanguage, "") = "" And Nz(Me.PrimaryDisability, "") = "" Then
          Exit Sub
  End If

  If Nz(Me.Gender, "") = "" Then
          MsgBox "Select Gender", vbInformation
          Cancel = True
          Exit Sub
     End If

    If Nz(Me.Race, "") = "" Then
          MsgBox "Select Race", vbInformation
          Cancel = True
          Exit Sub
     End If

     If Nz(Me.PrimaryLanguage, "") = "" Then
          MsgBox "Enter PrimaryLanguage", vbInformation
          Cancel = True
          Exit Sub
     End If

     If Nz(Me.PrimaryDisability, "") = "" Then
          MsgBox "Enter PrimaryDisability ", vbInformation
          Cancel = True
          Exit Sub
     End If
End Sub

推荐阅读