Search code examples
vbams-accesscomboboxlistboxmulti-select

How do I get combo boxes to become invisible when the selection is no longer selected in a multiselect list box?


I have a multiselect list box (nameDatabase) that when I select an option, a combo box will appear, and I can use it. However, when I unselect it, the combo box stays visible after it is unselected.

This is what I currently have to make them visible and place the items selected to a text box:

Private Sub nameDatabase_AfterUpdate()

Dim strEng As String
Dim varItem As Variant
    
For Each varItem In Me.nameDatabase.ItemsSelected
    
    strEng = strEng & ", " & Me.nameDatabase.ItemData(varItem)
        
Next
    
If Len(strEng) > 0 Then
    
    strEng = Mid(strEng, 2)
        
End If
    
Me.TestBox = strEng

If nameDatabase.Column(Value) = "A" Then
        
    codeJBox.Visible = True
        
ElseIf nameDatabase.Column(Value) = "B" Then
    
    codeSBox.Visible = True

ElseIf nameDatabase.Column(Value) = "C" Then

    codePBox.Visible = True
    codeABox.Visible = True
   
End If

End Sub

I have tried to have a text box have the selections in it and if, for example, "A" is not in there it will go away though it didn't work, or I did something wrong. I have a feeling it is something simple I'm missing to make the combo box disappear and that I'm overthinking everything.


Solution

  • This should work for your use-case (it didn't, see edit):

    Private Sub nameDatabase_Change()
        Dim Value As Long
        'Value = 1 'was for testing
        If nameDatabase.Text <> "" Then
            codeJBox.Visible = nameDatabase.Column(Value) = "A"
            codeSBox.Visible = nameDatabase.Column(Value) = "B"
            codePBox.Visible = nameDatabase.Column(Value) = "C"
            codeABox.Visible = nameDatabase.Column(Value) = "C"
        Else
            codeJBox.Visible = False
            codeSBox.Visible = False
            codePBox.Visible = False
            codeABox.Visible = False
        End If
    End Sub
    

    It turns the 4 invisible if nothing is selected and when something is typed only makes the ones you need visible. Let me know if something is unclear.

    EDIT: It appears I mistook your question for a normal listbox/combobox, not one from access. As such I can't test this further but you can try the following code which should still work. Let me know if it doesn't.

    Private Sub nameDatabase_AfterUpdate()
    
        Dim strEng As String
        Dim varItem As Variant
            
        For Each varItem In Me.nameDatabase.ItemsSelected
            If Len(strEng) = 0 Then
                strEng = Me.nameDatabase.ItemData(varItem)
            Else
                strEng = strEng & ", " & Me.nameDatabase.ItemData(varItem)
            End If
        Next varItem
            
        Me.TestBox = strEng
        
        codeJBox.Visible = InStr(strEng, "A") > 0
        codeSBox.Visible = InStr(strEng, "B") > 0
        codePBox.Visible = InStr(strEng, "C") > 0
        codeABox.Visible = InStr(strEng, "C") > 0
    End Sub