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

非 Y 不嫁゛ 提交于 2019-12-13 04:50:19

问题


How do I check to see if any of my 8 comboboxes match one another all at once (excluding null values of course, because they are all null when the form loads)? At the moment I have only figured out how to do it for the current and next one. In the case where there is a match, I want to clear the values of all the other comboboxes apart from the one in focus i.e. currentDropDown.

Code below:

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

回答1:


You need to loop through the collection of comboboxes and check currently selected value to the others.

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

usage:

Private Sub CombBox30_Change()
CheckValue CombBox30
End Sub


来源:https://stackoverflow.com/questions/27851145/check-to-see-if-8-comboboxes-contain-values-that-match-one-another-excluding-nul

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!