vba - MS Access Form VBA 检查复选框,在检查其他人之前?
问题描述
我正在开发一个带有 SQL 后端的 Access 应用程序。
我有一个人的表格,每个人都可能需要检查或取消检查不同的东西。表单上有以下字段和对应的控件:
无选项(整数 - 布尔值 - 复选框)
选项 A(整数 - 布尔值 - 复选框)
期权金额(金额)
选项 B(整数 - 布尔值 - 复选框)
选项 B 金额(金钱)
选项 C(整数 - 布尔值 - 复选框)
选项 C 金额(金钱)
选项 D(整数 - 布尔值 - 复选框)
选项 D 命令按钮(打开带有选项类型下拉列表和要输入的多个值的数量的弹出表单)。
我担心的是,如果有人选中一个框,它可能会与另一个复选框或货币字段发生冲突。
如果有人选中 No Options,并且选中了其他选项之一,我必须取消选中这些选项。我还需要将他们相应的货币字段设为 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
我还为 OptionA、OptionB、OptionC、OptionD 设置了更新前公共功能,如下所示:
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 属性的宏或函数正在阻止 [人员应用程序] 在该字段中保存数据。
有人知道我在做什么错吗?有没有更简单的方法来做到这一点?
谢谢!!!
解决方案
根据我最近的经验,当触发更新前事件时,它会在任何计算中使用该字段的新值。所以我希望你想要开始那个主要的If
陈述,比如If Me.NoOptions = True Then
. 我也不相信你需要sElse OptionCheck = True
之前的位End If
。
可能导致问题的是您调用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
推荐阅读
- angular - Angular:不支持这种类型作为注入令牌
- spring - Spring ACL中更新ACL的正确方法
- python - Python:在折线图中可视化数据并按类别分组
- javascript - 当添加透明时,mapbox 中的 setPaintProperty() 变为空白
- python - 如何从加载的检查点重置优化器状态
- google-apps-script - 突出显示 Google Sheets 脚本中的特定单词
- git - 格式化 Git %date 的问题
- python - 使用openpyxl,如何覆盖链接到数据透视表的特定excel表中的数据
- c# - 如何在 EF Core 配置中排除所有实体类型的属性?
- javascript - 可以在同一客户端和窗口上的两个应用程序之间建立 WebRTC 连接吗?