当组合框引用与表单不同的 table 时如何更新组合框

How to update combobox when it references a different table than the form

在我的“报价表”表单中,用户首先输入客户名称,这会导致表单中的其他字段(街道地址、买家等)自动填写与已知客户信息相对应的内容.客户信息存储在单独的链接 table“客户信息”中。在此表单中,一个特定字段“Buyer”由组合框“BuyerEntry”表示。组合框的下拉列表由“客户信息”中的三个字段填充 table - “买家(主要)”、“买家(次要)”和“买家(第三)”。

用户在表单中输入客户姓名后,如果他们将“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”事件,用户可以跳出该控件。

非常感谢有关如何解决此错误的任何建议and/or 进一步改进我的代码。谢谢

重新查询组合框
myComboBox.Requery