brianpowell
asked on
Collections question:
if
For Each item In SourceCollectnUnMatch
colTemp2.Add item
Next item
do something
then
colTemp2.remove item
I get a type mismatch error??? Does it have to do with the key?
For Each item In SourceCollectnUnMatch
colTemp2.Add item
Next item
do something
then
colTemp2.remove item
I get a type mismatch error??? Does it have to do with the key?
colTemp2.remove 1 'reomves 1st item in collection
....
colTemp2.remove 66 'removes 66th item in the collection
...
ASKER
interesting. I have been doing .remove item(i)
but why the type mismatch error?
if i use .add item, item
then .remove item
works well.
bp
but why the type mismatch error?
if i use .add item, item
then .remove item
works well.
bp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
interesting. I have been doing .remove item(i)
but why the type mismatch error?
========================== =======
Because remove method of collection expects an Index which is an LONG wairable.
so you cannot pass an object to it or type mismatch happens.
but why the type mismatch error?
==========================
Because remove method of collection expects an Index which is an LONG wairable.
so you cannot pass an object to it or type mismatch happens.
sorry for the typos lol
wairable=variable
wairable=variable
As I said before, the Remove method does not expect only an index value; it can also take a string value.
I don't think you have a full understanding of how the Key concept in a collection works. This small app demonstrates the use of key values in a collection. This code adds, updates and removes items in the collection all based upon key values...which are strings.
Create a new project and add a ListBox, two TextBoxes, and two CommandButtons. Paste the code below and hit run. Select the different pets in the listbox to see their data and use the buttons to add, update or remove pet's and their data.
Regards,
Idle_Mind
' -------------------------- ---------- ---------- ---------- ---------- ---
Option Explicit
Private petData As New Collection
Private Sub Form_Load()
Dim pet As String, desc As String
Command1.Caption = "Add/Update"
Command2.Caption = "Remove"
Text1.Text = ""
Text2.Text = ""
pet = "Dog"
desc = "Furry and Loveable"
petData.Add desc, pet
List1.AddItem pet
pet = "Cat"
desc = "Furry but Mean"
petData.Add desc, pet
List1.AddItem pet
pet = "Hamster"
desc = "Furry but Squashable"
petData.Add desc, pet
List1.AddItem pet
pet = "Fish"
desc = "Slimey but Cute"
petData.Add desc, pet
List1.AddItem pet
End Sub
Private Sub List1_Click()
' if a pet has been selected...
If List1.ListIndex <> -1 Then
' display the pet data in the textboxes
Text1.Text = List1.List(List1.ListIndex )
Text2.Text = petData.Item(List1.List(Li st1.ListIn dex))
End If
End Sub
Private Sub Command1_Click()
On Error GoTo addPet
Dim data As Variant
Dim i As Integer
' if we have valid pet data...
If Text1.Text <> "" And Text2.Text <> "" Then
data = petData.Item(Text1.Text)
' update the existing pet data
petData.Remove Text1.Text
petData.Add Text2.Text, Text1.Text
End If
Exit Sub
addPet:
' add the new pet along with it's data
petData.Add Text2.Text, Text1.Text
List1.AddItem Text1.Text
List1.ListIndex = List1.ListCount - 1
End Sub
Private Sub Command2_Click()
Dim removeIndex As Integer
' get the currently selected pet index
removeIndex = List1.ListIndex
If removeIndex <> -1 Then ' if a pet is actually selected...
' then remove the selected pet
petData.Remove List1.List(removeIndex)
List1.RemoveItem (removeIndex)
' Select a new current pet (if any left)
If removeIndex > List1.ListCount - 1 Then
removeIndex = List1.ListCount - 1
End If
If List1.ListCount > 0 Then
List1.ListIndex = removeIndex
Else
Text1.Text = ""
Text2.Text = ""
End If
End If
End Sub
I don't think you have a full understanding of how the Key concept in a collection works. This small app demonstrates the use of key values in a collection. This code adds, updates and removes items in the collection all based upon key values...which are strings.
Create a new project and add a ListBox, two TextBoxes, and two CommandButtons. Paste the code below and hit run. Select the different pets in the listbox to see their data and use the buttons to add, update or remove pet's and their data.
Regards,
Idle_Mind
' --------------------------
Option Explicit
Private petData As New Collection
Private Sub Form_Load()
Dim pet As String, desc As String
Command1.Caption = "Add/Update"
Command2.Caption = "Remove"
Text1.Text = ""
Text2.Text = ""
pet = "Dog"
desc = "Furry and Loveable"
petData.Add desc, pet
List1.AddItem pet
pet = "Cat"
desc = "Furry but Mean"
petData.Add desc, pet
List1.AddItem pet
pet = "Hamster"
desc = "Furry but Squashable"
petData.Add desc, pet
List1.AddItem pet
pet = "Fish"
desc = "Slimey but Cute"
petData.Add desc, pet
List1.AddItem pet
End Sub
Private Sub List1_Click()
' if a pet has been selected...
If List1.ListIndex <> -1 Then
' display the pet data in the textboxes
Text1.Text = List1.List(List1.ListIndex
Text2.Text = petData.Item(List1.List(Li
End If
End Sub
Private Sub Command1_Click()
On Error GoTo addPet
Dim data As Variant
Dim i As Integer
' if we have valid pet data...
If Text1.Text <> "" And Text2.Text <> "" Then
data = petData.Item(Text1.Text)
' update the existing pet data
petData.Remove Text1.Text
petData.Add Text2.Text, Text1.Text
End If
Exit Sub
addPet:
' add the new pet along with it's data
petData.Add Text2.Text, Text1.Text
List1.AddItem Text1.Text
List1.ListIndex = List1.ListCount - 1
End Sub
Private Sub Command2_Click()
Dim removeIndex As Integer
' get the currently selected pet index
removeIndex = List1.ListIndex
If removeIndex <> -1 Then ' if a pet is actually selected...
' then remove the selected pet
petData.Remove List1.List(removeIndex)
List1.RemoveItem (removeIndex)
' Select a new current pet (if any left)
If removeIndex > List1.ListCount - 1 Then
removeIndex = List1.ListCount - 1
End If
If List1.ListCount > 0 Then
List1.ListIndex = removeIndex
Else
Text1.Text = ""
Text2.Text = ""
End If
End If
End Sub
Ed