Solved

VB Collections..

Posted on 2000-03-30
10
276 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
ID: 2671269
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
ID: 2671357
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
ID: 2672312
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
ID: 2672718
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
ID: 2672721
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
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 1

Author Comment

by:Sieger
ID: 2673449
Yep.. you got it.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2673460
But I don't. Explain please

0
 
LVL 1

Author Comment

by:Sieger
ID: 2673767
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
ID: 2673851
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
ID: 2674509
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

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

919 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