MS Access表格VBA检查复选框,在检查其他人之前?

时间:2018-05-23 13:27:40

标签: access-vba dependencies boolean ms-access-2010 beforeupdate

我正在使用带有SQL后端的Access应用程序。

我有一个人的表格,每个人都需要检查或取消选中不同的东西。表单上有以下字段和相应的控件:

没有选项(整数 - 布尔 - 复选框)

选项A(整数 - 布尔 - 复选框)

选项A金额(金钱)

选项B(整数 - 布尔 - 复选框)

选项B金额(金钱)

选项C(整数 - 布尔 - 复选框)

选项C金额(金钱)

选项D(整数 - 布尔 - 复选框)

选项D命令按钮(打开弹出窗体,选项类型下拉列表,输入多个值的金额)。

我担心的是,如果有人检查一个方框,它可能会与另一个复选框或货币字段发生冲突。

如果有人选中No Options,并且选中了其他选项之一,我必须取消选中。我还需要把他们相应的钱场分开0。如果有选项D"其他选项"记录,在链接表中,然后我需要删除这些,在我与用户确认后。我还想禁用选项A-D的复选框和money / command按钮控件。

如果我取消选中No Options,那么我需要启用所有这些。

您可以开始查看每次检查任何无选项或选项A - D时,我必须检查无选项的状态以及选项的相应金额,以确认用户想做出这个改变。

为此,我为更新前的无选项设置了以下代码:

Private Sub NoOptions_BeforeUpdate(Cancel As Integer)
Dim Msg, Style, Title
Dim delOLiensSQL

If Me.NoOptions = False Then
    If (Me.OptionA = True Or Me.OptionB = True Or Me.OptionC = True Or Me.OptionD = True) Then
        Msg = "You have chosen No Options, but one or more option is checked." & vbCrLf & _
        "Choosing No Options will require removing all Lien amounts." & vbCrLf & _
        "Would you like to change this Person to No Options?"
        Style = vbYesNo
        Title = "All Options Will Be Reset to 0 and False."
        Response = MsgBox(Msg, Style, Title)

        If Response = vbYes Then
            If Nz(DLookup("ID", "tblPersonOtherOptionsD", "FKPerson = " & Me.ID), 0) Then
                delOLiensSQL = "Delete From tblPersonOtherOptionsD Where FKPerson = " & Me.ID
                DoCmd.RunSQL delOoptionssSQL, dbSeeChanges
            End If
            Me.OptionA = False
            Me.OptionAAmount = 0
            Me.OptionB = False
            Me.OptionBAmount = 0
            Me.OptionC = False
            Me.OptionCAmount = 0
            Me.OptionD = False
            OptionsAllowPubFunc (False)
        Else
            Me.Undo
            MsgBox "OK, we will leave everything as it is.", vbOKOnly, "Better Safe Than Sorry"
        End If
    Else
        Me.NoOptions = True
    End If
Else
    Me.NoOptions = False
End If
End Sub

OptionsAllowPubFunc(False)是一个公共函数,如下所示:

Public Function PlaintiffLiensAllowed(Liens As Boolean)
    Forms!frmPerson.OptionAAmount.Enabled = Liens
    Forms!frmPerson.OptionBAmount.Enabled = Liens
    Forms!frmPerson.OptionCAmount.Enabled = Liens
    Forms!frmPerson.OptionDAmount.Enabled = Liens
End Function

我还为OptionA,OptionB,OptionC,OptionD设置了Before Update公共功能,如下所示:

Public Function ChangeAOption(OptionCheck As Control, OptionAmount As Control, OptionName As String)
    Dim Msg, Style, Title
    Dim Msg2, Style2, Title2

    If OptionCheck = True Then

        If Nz(OptionAmount, 0) > 0 Then
            Msg = "There is a " & OptionName & " Option amount. Unchecking " & OptionName & " Option, will require the amount to be 0." & vbCrLf & _
            "Would you like to uncheck " & OptionName & " Option and make the amount 0?"
            Style = vbYesNo
            Title = "Confirm No " & OptionName & " Option."
            Response = MsgBox(Msg, Style, Title)

            If Response = vbYes Then
                OptionAmount = 0
                OptionCheck = False
            Else
                OptionCheck.Undo
                MsgBox "Ok, we will leave it as is.", vbOKOnly, "Better Safe Than Sorry."
            End If
        Else
            OptionCheck = False
        End If
    Else
        If Forms!frmPerson.NoOptions = True Then
            Msg2 = "No Options is Checked. Checking " & OptionName & " Options will require no Options to be unchecked." & vbCrLf & _
            "Would you like to uncheck no Options?"
            Style2 = vbYesNo
            Title2 = "Confirm No Options False."
            Response2 = MsgBox(Msg2, Style2, Title2)
            If Response2 = vbYes Then
                OptionsAllowPubFunc (True)
                Forms!frmPerson.NoOptions = False
                OptionCheck = True
            Else
                OptionCheck = True
            End If
        Else
        OptionCheck = True
    End If
End Function

我正在对此进行测试,当我尝试选中No Options复选框时,将其从false更改为true,我收到运行时错误' -2147352567(80020009)':

  

将宏或函数设置为BeforeUpdate或ValidationRule属性   对于此字段阻止[人员应用程序]保存数据   在现场。

任何人都知道我做错了什么?有更简单的方法吗?

谢谢!!!

1 个答案:

答案 0 :(得分:0)

根据我最近的经验,当触发Before Update事件时,它会在任何计算中使用字段的新值。所以我希望你想要开始像If这样的主If Me.NoOptions = True Then语句。 我也不相信你需要Else OptionCheck = True之前的End If位。

可能导致问题的原因是您已拨打OptionsAllowPubFunc,但您在此问题中包含的功能实际上称为PlaintiffLiensAllowed - 除非您有相同的功能OptionsAllowPubFunc,这个' ll会给你一个错误。

最后,我从未发现me.undo在这种情况下特别有帮助,但是更新之前为您提供了一个 - 只需告诉它取消使用Cancel = True(或任何内容)那不是0)并且它不会更新字段并退出子。

如果他们取消选中该框,您还可以添加一些内容来重新启用选项字段。

尝试将其作为:

运行
Private Sub NoOptions_BeforeUpdate(Cancel As Integer)
Dim Msg As String, Style As VbMsgBoxStyle, Title As String
Dim Response As VbMsgBoxResult, delOLiensSQL As String

If Me.NoOptions = True Then
    If (Me.OptionA = True Or Me.OptionB = True Or Me.OptionC = True Or Me.OptionD = True) Then
        Msg = "You have chosen No Options, but one or more option is checked." & vbCrLf & _
        "Choosing No Options will require removing all Lien amounts." & vbCrLf & _
        "Would you like to change this Person to No Options?"
        Style = vbYesNo
        Title = "All Options Will Be Reset to 0 and False."
        Response = MsgBox(Msg, Style, Title)

        If Response = vbYes Then
            If Nz(DLookup("ID", "tblPersonOtherOptionsD", "FKPerson = " & Me.ID), 0) Then
                delOLiensSQL = "Delete From tblPersonOtherOptionsD Where FKPerson = " & Me.ID
                DoCmd.RunSQL delOoptionssSQL, dbSeeChanges
            End If
            Me.OptionA = False
            Me.OptionAAmount = 0
            Me.OptionB = False
            Me.OptionBAmount = 0
            Me.OptionC = False
            Me.OptionCAmount = 0
            Me.OptionD = False
            PlaintiffLiensAllowed(False)
        Else
            MsgBox "OK, we will leave everything as it is.", vbOKOnly, "Better Safe Than Sorry"
            Cancel = True
        End If
    End If
Else
    PlaintiffLiensAllowed(True)
End If
End Sub