如何检查我的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
答案 0 :(得分:1)
您需要遍历组合框的集合并将当前选定的值检查到其他组合。
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