MS 访问表单 VBA 复选框,在检查其他复选框之前?



我正在开发一个带有SQL后端的Access应用程序。

我有一个针对人们的表格,每个人都可能需要检查或取消检查不同的东西。窗体上有以下字段和相应的控件:

无选项(整数 - 布尔值 - 复选框(

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

选项 A 金额(金额(

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

选项 B 金额(金额(

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

选项 C 金额(金额(

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

选项D 命令按钮(打开弹出表单,其中包含选项类型下拉列表和要输入的多个值的金额(。

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

如果有人选中"无选项",并且选中了其他选项之一,则我必须取消选中这些选项。我还需要 0 出他们相应的钱字段。如果链接表中有选项 D"其他选项"记录,那么在与用户确认后,我也需要删除这些记录。我还想禁用选项 A - D 的复选框和金钱/命令按钮控件。

如果我取消选中"无选项",则需要启用所有这些选项。

您可以开始看到每次我选中任何"无选项"或"选项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

我还为选项 A、选项 B、选项C、选项D 设置了更新前公共函数,如下所示:

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

我正在测试这个,当我尝试选中"无选项"复选框并将其从 false 更改为 true 时,我收到运行时错误"-2147352567 (80020009(":

设置为"更新前"或"验证规则"属性的宏或函数 因为此字段阻止 [People 应用程序] 保存数据 在现场。

有人知道我做错了什么吗? 有没有更简单的方法可以做到这一点?

谢谢!!!

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

可能导致问题的是您调用了OptionsAllowPubFunc,但您在此问题中包含的函数实际上称为PlaintiffLiensAllowed- 除非您有一个名为 OptionsAllowPubFunc 的相同函数,否则这会给您一个错误。

最后,我从未发现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

相关内容

最新更新