访问:禁用组合框

时间:2018-12-28 09:25:53

标签: ms-access combobox

我的表单中有5个组合框,我希望用户只能选择2个。一旦选择2个,其他3个将被禁用吗?我该怎么办?我知道您可以做一个组合框,并通过afterupdate禁用其余的组合,如下所示。感谢您的帮助! :)

Private Sub cboOR_AfterUpdate()
Me.cboA.Enabled = False
End Sub

2 个答案:

答案 0 :(得分:0)

将此Sub放入表单模块

Private Sub changeStateOfCB()

    Dim nameChB() As String
    Dim cMax As Long
    Dim ctrl As Control
    Dim cValued As Long
    Dim nameCurr As Variant

    ' names of CB
    nameChB = Split("ComboName1#ComboName2#ComboName3#ComboName4#ComboName5", "#")
    ' max allowed values
    cMax = 2

    ' counting with values
    For Each nameCurr In nameChB
        If Not Me.Controls(nameCurr).Value Is Null Then
            cValued = cValued + 1
        End If
    Next
    ' disabling if needed
    For Each nameCurr In nameChB
        If Not Me.Controls(nameCurr).Value Is Null Then
            Me.Controls(nameCurr).Enabled = (cValued < cMax)
        End If
    Next

End Sub

根据您在nameChB行中的名称更改字符串中的名称。 并为每个组合框创建带有调用

AfterUpdate事件过程
changeStateOfCB

答案 1 :(得分:0)

签出

Private Sub Combo0_AfterUpdate()
    If CheckCombo Then
        disableCombo
    Else
        enableCombo
    End If
End Sub



Private Sub Combo10_AfterUpdate()
  If CheckCombo Then
        disableCombo
    Else
        enableCombo
    End If
End Sub

Private Sub Combo12_AfterUpdate()
  If CheckCombo Then
        disableCombo
    Else
        enableCombo
    End If
End Sub

Private Sub Combo14_AfterUpdate()
  If CheckCombo Then
        disableCombo
    Else
        enableCombo
    End If
End Sub

Private Sub Combo16_AfterUpdate()
  If CheckCombo Then
        disableCombo
    Else
        enableCombo
    End If
End Sub


Private Function CheckCombo() As Boolean

  Dim retVal As Long
  retVal = IIf(Len(Nz(Combo0, "")) > 0, 1, 0) + IIf(Len(Nz(Combo10, "")) > 0, 1, 0) + IIf(Len(Nz(Combo12, "")) > 0, 1, 0) + IIf(Len(Nz(Combo14, "")) > 0, 1, 0) + IIf(Len(Nz(Combo16, "")) > 0, 1, 0)
  CheckCombo = (retVal >= 2)

End Function

Private Sub enableCombo()
    Combo0.Enabled = True
    Combo10.Enabled = True
    Combo12.Enabled = True
    Combo14.Enabled = True
    Combo16.Enabled = True
End Sub

Private Sub disableCombo()
    If Len(Nz(Combo0, "")) <= 0 Then Combo0.Enabled = False
    If Len(Nz(Combo10, "")) <= 0 Then Combo10.Enabled = False
    If Len(Nz(Combo12, "")) <= 0 Then Combo12.Enabled = False
    If Len(Nz(Combo14, "")) <= 0 Then Combo14.Enabled = False
    If Len(Nz(Combo16, "")) <= 0 Then Combo16.Enabled = False
End Sub