Solved

VB Collections..

Posted on 2000-03-30
10
277 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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
 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

770 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