MsgBox or How to avoid adding a second time
MsgBox or How to avoid adding a second time
(OP)
Hi,
I've two list box, list1 and list2. When a user selects an item from list1 and click on a command button, the item is added to list2, without be delete from list1. No problem with that.
What I want to do now is when the user will tries to add item from list1 a second time, a message box will pop up to say something like "Item already choosen." and the item will not be added to list2. This is a sample of my code:
Private Sub Addition_Click()
Dim ctlList As Control
Dim ctlList2 As Control
Dim varitem As Variant
Dim strSQL As String
Set ctlList = Me.list1
Set ctlList2 = Me.list2
For Each varitem In ctlList.ItemsSelected
If 'Selected item in list1 is already in list2' Then
MsgBox "Item already choosen."
Exit sub
Else
'Insert item in list2. No problem with this.
End If
Next varitem
End Sub
My problem is that I don't know what could be the condition of my IF instruction. Anyone has ideas?
Any help is welcome,
Frank
I've two list box, list1 and list2. When a user selects an item from list1 and click on a command button, the item is added to list2, without be delete from list1. No problem with that.
What I want to do now is when the user will tries to add item from list1 a second time, a message box will pop up to say something like "Item already choosen." and the item will not be added to list2. This is a sample of my code:
Private Sub Addition_Click()
Dim ctlList As Control
Dim ctlList2 As Control
Dim varitem As Variant
Dim strSQL As String
Set ctlList = Me.list1
Set ctlList2 = Me.list2
For Each varitem In ctlList.ItemsSelected
If 'Selected item in list1 is already in list2' Then
MsgBox "Item already choosen."
Exit sub
Else
'Insert item in list2. No problem with this.
End If
Next varitem
End Sub
My problem is that I don't know what could be the condition of my IF instruction. Anyone has ideas?
Any help is welcome,
Frank
RE: MsgBox or How to avoid adding a second time
RE: MsgBox or How to avoid adding a second time
All I want is a msg box who will indicate that the selected item already exists inlist2. Is it possible?
Thanks in advance for your help,
Cantor
RE: MsgBox or How to avoid adding a second time
You don't need to add "live" data to the list box. You can step through a recordset and get the data and then add it to your list1. Then it doesn't matter if the data is removed.
Let us know if you want details or check out the Knowledge Base article Elizabeth mentioned above, it's Q177117.
Kathryn