Solved

VB Collections..

Posted on 2000-03-30
10
280 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
[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
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 28

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

724 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