需要验证码,因此只有3个布尔字段中的1个为真

时间:2013-05-27 22:42:24

标签: forms validation ms-access boolean ms-access-2010

Access 2010:我有一个包含3个布尔字段的表 - 称之为Field_A,Field_B和Field_C。

在数据输入表单上,用户应该能够检查(使值为TRUE)这些选项中的任何一个,但是任何时候只有一个选项可以为TRUE。如果Field_B已经为true并且用户想要更改它,那么Field_C是选择为TRUE的选项,他首先必须取消选择Field_B(将其重置为FALSE),然后才能检查Field_C表单上的框。

所以我需要为每个字段提供一些验证码,如果用户试图将一个字段设置为TRUE,则检查其他两个字段的状态。如果其他两个字段当前都为FALSE,则允许将当前字段更改为TRUE。但是,如果其他任何一个字段当前为TRUE,它应该创建一个弹出消息,说明已经有另一个选择,另一个字段必须首先更改为FALSE才能继续。

我尝试使用Yes / No选项的数值,在允许感兴趣的字段(例如Field_A)更改为TRUE(值)之前,设置需要其他两个值之和为零的条件验证= -1)(就像([Field_B] + [Field_C]) =0之类的东西,但我一直遇到语法错误。我已经足够新了,我不知道它是否只是一个简单的语法问题,或者是一个完全不同的方法需要。

最后一条信息 - 将所有3个字段设置为FALSE是可以接受的,因此如果另一个字段从TRUE更改为FALSE,我不希望强制其中一个字段变为TRUE。

2 个答案:

答案 0 :(得分:0)

您的数据输入表单背后的一些代码应该可以解决问题。尝试以下几点:

Option Compare Database
Option Explicit

Private Sub Field_A_BeforeUpdate(Cancel As Integer)
    Const ThisField = "Field_A"
    If Me.Field_A.Value Then
        If Me.Field_B.Value Then
            ShowMyMessage "Field_B", ThisField
            Cancel = True
        ElseIf Me.Field_C.Value Then
            ShowMyMessage "Field_C", ThisField
            Cancel = True
        End If
    End If
End Sub

Private Sub Field_B_BeforeUpdate(Cancel As Integer)
    Const ThisField = "Field_B"
    If Me.Field_B.Value Then
        If Me.Field_A.Value Then
            ShowMyMessage "Field_A", ThisField
            Cancel = True
        ElseIf Me.Field_C.Value Then
            ShowMyMessage "Field_C", ThisField
            Cancel = True
        End If
    End If
End Sub

Private Sub Field_C_BeforeUpdate(Cancel As Integer)
    Const ThisField = "Field_C"
    If Me.Field_C.Value Then
        If Me.Field_B.Value Then
            ShowMyMessage "Field_B", ThisField
            Cancel = True
        ElseIf Me.Field_A.Value Then
            ShowMyMessage "Field_A", ThisField
            Cancel = True
        End If
    End If
End Sub

Private Sub ShowMyMessage(OtherField As String, CurrentField As String)
    MsgBox _
            "You must un-select """ & OtherField & """" & _
                " before you can select """ & CurrentField & """", _
            vbExclamation, _
            "Mutually exclusive options conflict"
End Sub

答案 1 :(得分:0)

这3个复选框有两个可接受的组合:

  1. 全部为False(未选中)
  2. 只有一个可以是True(已选中)
  3. 这意味着这些复选框值的总和必须为0或-1。

    ? False + False + False
     0 
    ? False + False + True
    -1 
    

    您可以在表单的代码模块中添加一个函数...

    Private Function checkBoxProblem() As Boolean
        Dim blnReturn As Boolean
        Dim intSum As Integer
        Dim strPrompt As String
    
        intSum = Nz(Me.Field_A, 0) + Nz(Me.Field_B, 0) + Nz(Me.Field_C, 0)
        If Not (intSum = 0 Or intSum = -1) Then
            strPrompt = "only one box can be checked"
            MsgBox strPrompt
            blnReturn = True
        Else
            blnReturn = False
        End If
        checkBoxProblem = blnReturn
    End Function
    

    然后从这3个复选框中每个复选框的更新前事件中调用该函数。

    Private Sub Field_A_BeforeUpdate(Cancel As Integer)
        Cancel = checkBoxProblem
    End Sub
    Private Sub Field_B_BeforeUpdate(Cancel As Integer)
        Cancel = checkBoxProblem
    End Sub
    Private Sub Field_C_BeforeUpdate(Cancel As Integer)
        Cancel = checkBoxProblem
    End Sub