Solved

Collections question:

Posted on 2004-04-02
8
157 Views
Last Modified: 2010-05-02
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?  







0
Comment
Question by:brianpowell
8 Comments
 
LVL 4

Expert Comment

by:SSSoftware
ID: 10745109
You don't remove an item , You remove an Index of an item in the collextion or a Key of an item in the collection.

Ed
0
 
LVL 15

Expert Comment

by:unknown_routine
ID: 10745465

colTemp2.remove 1  'reomves 1st item in collection
....


colTemp2.remove 66  'removes 66th item in the collection

...
0
 
LVL 1

Author Comment

by:brianpowell
ID: 10747602
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 50 total points
ID: 10747652
You can remove items from a collection by index OR by key.  If the value passed in to the Remove method is a string, then the collection attempts to find the corresponding item that matches that key.  Since you didn't add anything to the colTemp2 collection with a key, you can only remove by index.

colTemp2.Add item ' Added WITHOUT a key
colTemp2.Add item, item ' Added WITH a key

Idle_Mind
0
 
LVL 15

Expert Comment

by:unknown_routine
ID: 10748567
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.







0
 
LVL 15

Expert Comment

by:unknown_routine
ID: 10748620
sorry for the typos lol

wairable=variable
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 10749497
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(List1.ListIndex))
    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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Controlling which port to download from 4 72
Vb.net 2008 2 56
VB6 Compile Compatibility Issue 4 90
to transfer string from C lanaguage to VBA 4 58
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now