检查 8 个组合框是否包含相互匹配的值,不包括 null

Check to see if 8 comboboxes contain values that match one another excluding null

我如何检查我的 8 个组合框中的任何一个是否同时相互匹配(当然不包括空值,因为它们在加载表单时都是空的)?目前我只知道如何为当前和下一个做这件事。在匹配的情况下,我想清除除焦点之外的所有其他组合框的值,即 currentDropDown.

代码如下:

Private Sub Form_Load()
cboOption2.Enabled = False
cboOption3.Enabled = False
cboOption4.Enabled = False
cboOption6.Enabled = False
cboOption7.Enabled = False
cboOption8.Enabled = False   
cboOption1.Value = Null
cboOption2.Value = Null
cboOption3.Value = Null
cboOption4.Value = Null
cboOption5.Value = Null
cboOption6.Value = Null
cboOption7.Value = Null
cboOption8.Value = Null  
End Sub

Sub rTotal(currentDropDown, nextDropDown)
If (currentDropDown.Value = nextDropDown.Value) Then
    MsgBox "You cannot select the same value twice."
    currentDropDown.Value = Null
End If
End Sub

Private Sub cboOption1_Change()
Call rTotal(cboOption1, cboOption2)
End Sub

Private Sub cboOption2_Change()
Call rTotal(cboOption2, cboOption3)
End Sub

Private Sub cboOption3_Change()
Call rTotal(cboOption3, cboOption4)
End Sub

Private Sub cboOption4_Change()
Call rTotal(cboOption4, cboOption5)
End Sub

Private Sub cboOption5_Change()
Call rTotal(cboOption5, cboOption6)
End Sub

Private Sub cboOption6_Change()
Call rTotal(cboOption6, cboOption7)
End Sub

Private Sub cboOption7_Change()
Call rTotal(cboOption7, cboOption8)
End Sub

Private Sub cboOption8_Change()
Call rTotal(cboOption8, cboOption8)
End Sub

您需要遍历组合框集合并将当前选择的值检查到其他值。

Sub CheckValue(ByVal currCombobox As ComboBox)
Dim ctl As Control, cmb As ComboBox
    For Each ctl In Me.Controls
        If ctl.ControlType = acComboBox Then
            Set cmb = ctl
            If (currCombobox.Value = cmb.Value) And (Not currCombobox Is cmb) Then
                MsgBox "Cannot select it twice!" & vbcr & vbcr & _
                        currCombobox.Name & " = " &  cmb.Name
            End If
        End If
    Next ctl
Set ctl = Nothing

End Sub

用法:

Private Sub CombBox30_Change()
CheckValue CombBox30
End Sub