检查组合框值是否匹配两次以上

时间:2015-01-09 00:22:10

标签: vba combobox access-vba ms-access-2010

如果特定组合框值在我的控件集中出现超过3次(“md”),我试图触发事件。然而,目前,尽管我的Access窗体上只有8个组合框,但我仍然保持32或40等高值。我做错了什么?

Dim mdCount As Integer
For Each ctl In Me.Controls
    If ctl.ControlType = acComboBox Then
        Set cmb = ctl
        If (currentDropDown.Value = cmb.Value) And (Not currentDropDown Is cmb) And (Not currentDropDown.Value = "md") Then
            MsgBox "You cannot select the same value twice."
        End If
        If (currentDropDown.Value = "md") Then
            mdCount = mdCount + 1
        End If
    End If
Next ctl
Set ctl = Nothing

Private Sub Submit_Click()
'MsgBox mdCount
If (mdCount > 2) Then 
    MsgBox "Error!"
    Exit Sub
End Sub

1 个答案:

答案 0 :(得分:1)

如果我理解正确的话......尝试这样的事情(有点硬编码,但非常快):

Function CheckMatches() As Integer
Dim sTmp As String

sTmp = IIf(Nz(Me.Combo1.Value, "") = "md", ";", "") & _
        IIf(Nz(Me.Combo2.Value, "") = "md", ";", "") & _
        IIf(Nz(Me.Combo3.Value, "") = "md", ";", "")  'and so on...

CheckMatches = UBound(Split(sTmp, ";")) + 1
'+1 is necessary in case of Option Base 0, _
'because LBound(array) starts from 0

End Function

用法:

Private Sub Submit_Click()
Dim mdCount as Integer
mdCount = CheckMatches
If (mdCount > 2) Then 
    MsgBox "Error!"
    Exit Sub
End Sub

您的要求不明确,所以我无法提供更多帮助;(