Solved

VB Collections..

Posted on 2000-03-30
10
278 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 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS expression Issue finding a string 10 91
Best way to parse out a json string in VB6? 10 224
Hide vba in gp 7 104
VBA Shell can't Find Word document 11 112
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

828 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