Solved

VB Collections..

Posted on 2000-03-30
10
275 Views
Last Modified: 2010-05-02
I am trying to use Collection in my App..

Dim Numbers as New Collection.
for i = 1 to 10
Numbers.Add i,i
next

Numbers.Count works fine..
but when I tried to do a Numbers.Item(1)
it gives me an error...
Object is no longer valid....
Any Idea?
0
Comment
Question by:Sieger
10 Comments
 
LVL 32

Expert Comment

by:Erick37
Comment Utility
Option Explicit

Dim cNumbers As New Collection

Private Sub Command1_Click()
    Debug.Print cNumbers.Item(5)
End Sub

Private Sub Form_Load()
    Dim i As Long
    For i = 1 To 10
        cNumbers.Add i
    Next
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Set cNumbers = Nothing
End Sub
0
 
LVL 27

Expert Comment

by:Ark
Comment Utility
Hi
Second argument in .Add method is a key (string). If you want to use it, change your code:
Dim Numbers as New Collection.
for i = 1 to 10
    Numbers.Add i,cstr(i)
next
And then you can use:
Numbers.Item(1)
or
Numbers.Item("1")
Cheers
0
 
LVL 1

Accepted Solution

by:
jbathija earned 50 total points
Comment Utility
The second argument should be a string value so may be you could try something like this and i have tested it and it works

check this



Dim Numbers As New Collection
Dim i As Integer
For i = 1 To 10
Numbers.Add i, "a" & i
Next

MsgBox Numbers.Count
MsgBox Numbers.Item(1)
0
 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
Exactly. What I don't understand is how you did not get a type mismatch on the line

Numbers.Add i,i

This should happen even without Option Explicit.

Is there something you're not telling us?

Perhaps the whole chunk of code:
Dim Numbers as New Collection.
for i = 1 to 10
Numbers.Add i,i
next
doesn't even get executed. In which case you're looking at an empty collection. After which it is obvious to see why
MsgBox Numbers.Item(1) doesn't work

But... Object is no longer valid? Again, that does NOT make sense.

I think we'll need to see a lot more of your code before we can come to a definitive conclusion about what's wrong with it (that's a hint to jbathija)

0
 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
Yep. With the simple code:

Dim Numbers As New Collection
Msgbox Numbers.Item(1) you get an error 5: Invalid procedure call or argument

With the following code:

Dim numbers As New Collection

numbers.Add 1, "1"

numbers.Item(1) = "Fred"

MsgBox numbers.Item(1)

You get error 424: object required.

So.... we *do* need more information.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:Sieger
Comment Utility
Yep.. you got it.
0
 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
But I don't. Explain please

0
 
LVL 1

Author Comment

by:Sieger
Comment Utility
heh.... yeah sorry it's all my bad... I am actually assigning the value and key with recordset values... I made 2 mistakes.. 1) I assigned the value with
Recordset instead of Recordset.value
2) I assigned integers to keys..
0
 
LVL 1

Author Comment

by:Sieger
Comment Utility
heh.... yeah sorry it's all my bad... I am actually assigning the value and key with recordset values... I made 2 mistakes.. 1) I assigned the value with
Recordset instead of Recordset.value
2) I assigned integers to keys..
0
 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
Aha, I see! Now I understand. Thanks for the explanation!

So the "Object is no longer valid" error was caused by recordset problems.

Phew, I thought I was losing it there for a moment :o)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…

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

14 Experts available now in Live!

Get 1:1 Help Now