Solved

Collections question:

Posted on 2004-04-02
8
156 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

762 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

20 Experts available now in Live!

Get 1:1 Help Now