Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Collections question:

Posted on 2004-04-02
8
Medium Priority
?
166 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 200 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 86

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

671 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