Reputation: 926
Case: I have a CommandButton and an unbound ComboBox in a form (RowSourceType: list of values - populated from another form using VBA).
Goal: When a user clicks on the CommandButton, the selected item in the ComboBox to be deleted from this ComboBox.
Attempts: I used in the Click event of the CommandButton, the RemoveItem method of the ComboBox, which needs the index of the item-to-delete. To get the index of the selected item, I tried to use the Selected property of the ComboBox, looping through all the ComboBox items, but the Selected property keeps returning 0 regrdless of the selection.
Private Sub bDelete_Click()
Dim i As Integer
For i = 0 To Me.cAnswered.ListCount - 1
If Me.cAnswered.Selected(i) = True Then
'MsgBox i
'Stop
Me.cAnswered.RemoveItem i
Exit For
End If
Next
Me.bDelete.Visible = (Me.cAnswered.ListCount > 0)
End Sub
Can you please tell me how can I achieve this goal?
Upvotes: 2
Views: 1177
Reputation: 12353
Try this
Private Sub bDelete_Click()
Dim i As Integer
For i = 0 To Me.cAnswered.ListCount - 1
If Me.cAnswered.ItemData(i) = cAnswered.Value Then
Me.cAnswered.RemoveItem i
Exit For
End If
Next
cAnswered = Null
End Sub
Upvotes: 2