当它引用与表单不同的表时如何更新组合框
Posted
技术标签:
【中文标题】当它引用与表单不同的表时如何更新组合框【英文标题】:How to update combobox when it references a different table than the form 【发布时间】:2020-07-31 15:17:17 【问题描述】:在我的“报价单”表单中,用户首先输入客户名称,这会导致表单中的其他字段(街道地址、买家等)自动填充与已知客户信息对应的内容。客户信息存储在单独的链接表“客户信息”中。在这种形式中,一个特定的字段“Buyer”由组合框“BuyerEntry”表示。组合框的下拉列表由“客户信息”表中的三个字段填充 - “买方(初级)”、“买方(次级)”和“买方(第三级)”。
用户在表单中输入客户姓名后,如果他们将“BuyerEntry”控件的自动填充信息编辑为与“客户信息”中的三个买方字段中的任何一个的现有信息不同的内容,则系统询问用户是否愿意更新“客户信息”中的信息以反映他们输入的信息。
我的代码如下所示。本质上,它检查“客户信息”中是否有任何打开的“买家”字段可以添加数据。如果所有这些字段都已满,则用户可以使用他们输入的新信息更新任何字段。
Private Sub BuyerEntry_NotInList(NewData As String, Response As Integer)
'Check for match with existing customer information and update if necessary
Dim MsgBoxAnswer As Variant
Response = acDataErrContinue
'Request Permission to update customer information based on conflicting entries
MsgBoxAnswer = MsgBox("The buyer does not match what is currently listed for this customer. Would you like to update the customer's information to match?", vbYesNo, "Edit Customer Information?")
If MsgBoxAnswer = vbNo Then 'No permission granted to update information
Me.BuyerEntry = Null
Exit Sub
Else 'Permission granted to update information
If IsNull(DLookup("[Buyer (Primary)]", "[Customer Information]", "[Customer Name] = CustomerEntry.Value")) = True Then 'If the primary buyer position is empty, update the customer information to include the entered buyer here
DoCmd.RunSQL "UPDATE [Customer Information] SET [Buyer (Primary)] = '" & NewData & "' WHERE [Customer Name] = '" & Form.Controls("CustomerEntry") & "'"
Exit Sub
ElseIf IsNull(DLookup("[Buyer (Secondary)]", "[Customer Information]", "[Customer Name] = CustomerEntry.Value")) = True Then 'If the secondary buyer position is empty, update the customer information to include the entered buyer here
DoCmd.RunSQL "UPDATE [Customer Information] SET [Buyer (Secondary)] = '" & NewData & "' WHERE [Customer Name] = '" & Form.Controls("CustomerEntry") & "'"
Exit Sub
ElseIf IsNull(DLookup("[Buyer (Tertiary)]", "[Customer Information]", "[Customer Name] = CustomerEntry.Value")) = True Then 'If the tertiary buyer position is empty, update the customer information to include the entered buyer here
DoCmd.RunSQL "UPDATE [Customer Information] SET [Buyer (Tertiary)] = '" & NewData & "' WHERE [Customer Name] = '" & Form.Controls("CustomerEntry") & "'"
Exit Sub
Else 'If all buyer positions are already filled, ask the user if they would like to update the primary buyer position
MsgBoxAnswer = MsgBox("Would you like to update this to be the customer's primary buyer?", vbYesNo, "Update Primary Buyer?")
If MsgBoxAnswer = vbYes Then 'Permission granted to update primary buyer position
DoCmd.RunSQL "UPDATE [Customer Information] SET [Buyer (Primary)] = '" & NewData & "' WHERE [Customer Name] = '" & Form.Controls("CustomerEntry") & "'"
Exit Sub
Else 'No permission granted to update primary buyer position. Ask the user if they would like to update the secondary buyer position
MsgBoxAnswer = MsgBox("Would you like to update this to be the customer's secondary buyer?", vbYesNo, "Update Secondary Buyer?")
If MsgBoxAnswer = vbYes Then 'Permission granted to update secondary buyer position
DoCmd.RunSQL "UPDATE [Customer Information] SET [Buyer (Secondary)] = '" & NewData & "' WHERE [Customer Name] = '" & Form.Controls("CustomerEntry") & "'"
Exit Sub
Else 'No permission granted to update secondary buyer position. Ask the user if they would like to update the tertiary buyer position
MsgBoxAnswer = MsgBox("Would you like to update this to be the customer's tertiary buyer?" & vbCrLf & "Note: This is the last opportunity to update the customer's buyer information.", vbYesNo, "Update Tertiary Buyer?")
If MsgBoxAnswer = vbYes Then 'Permission granted to update tertiary buyer position
DoCmd.RunSQL "UPDATE [Customer Information] SET [Buyer (Tertiary)] = '" & NewData & "' WHERE [Customer Name] = '" & Form.Controls("CustomerEntry") & "'"
Exit Sub
Else 'No Permission granted to update tertiary buyer position
Me.BuyerEntry = Null 'Make the list control empty for the time being
Exit Sub
End If
End If
End If
End If
End If
End Sub
代码不会产生任何错误。但是,在用户更新“客户信息”中的信息后,组合框的下拉列表不会更改以反映此更新的信息。结果,它再次触发了“NotInList”事件,因此,除非用户单击表单上的控件外,否则用户将继续循环执行此代码,此时控件会更新。我希望控件中的下拉列表在用户更改“客户信息”后立即更新,这样不会触发“NotInList”事件,并且用户可以跳出此控件。
感谢有关如何解决此错误和/或进一步改进我的代码的任何建议。谢谢
【问题讨论】:
【参考方案1】:重新查询组合框
myComboBox.Requery
【讨论】:
我试过了。但是,当我添加 Requery 命令时,我收到以下错误:“运行时错误 '2118':您必须在运行 Requery 操作之前保存当前字段。”然后,当我尝试使用“Forms![Quote Form].Dirty = False”保存表单时,它给了我一个错误,指出“找不到当前记录” 尝试重新分配组合框的 RowSource。以上是关于当它引用与表单不同的表时如何更新组合框的主要内容,如果未能解决你的问题,请参考以下文章