|
-
Oct 24th, 2001, 05:45 AM
#1
Thread Starter
Addicted Member
Object no longer valid
Hi,
This is the error i receive when i am using collections. I am adding numeric value with string value as the key in collection. Now when i retrieve these values i receive this error.
Now i worked lot on this and finally i was able to solve the probs by concatenating the value with & "" but i'm not able to find the logic behind it.
If anybody has any idea.
Thanks.
-
Oct 24th, 2001, 06:02 AM
#2
Retired VBF Adm1nistrator
Well I dunno if this is relevant, but you cannot use an entirely numeric value as a key. I think it either must have a string part, or have a string part prefixed.
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Oct 24th, 2001, 06:06 AM
#3
Thread Starter
Addicted Member
ya thats fine numeric vals can't be used as key and in my case i am using string value as key.
-
Oct 24th, 2001, 06:28 AM
#4
Show us some code, and you could properly get alot more help
-
Oct 24th, 2001, 06:35 AM
#5
Thread Starter
Addicted Member
'Initializing collection
Set mcolVAS = New Collection
'Adding items in collections based on recordset
'vms_id is NUMERIC & vms_name is STRING
Do While Not lrsVAS.EOF
mcolVAS.Add lrsVAS!vms_id , UCase(lrsVAS!vms_name)
lrsVAS.MoveNext
Loop
Set lrsVAS = Nothing
'Reading from collection
lstrValues = mcolVAS.Item(rsOriginalData!MVS_SERVICE) & ","
Here it gives error, OBJECT NO LONGER VALID.
Now if i change upper line as
mcolVAS.Add CStr(lrsVAS!vms_id & "") , UCase(lrsVAS!vms_name)
It runs perfect.
Thanks.
-
Oct 24th, 2001, 06:43 AM
#6
Could you please test this, and see if it does the trick (that is work!)
VB Code:
Do While Not lrsVAS.EOF
mcolVAS.Add CLng(lrsVAS!vms_id) , UCase(lrsVAS!vms_name)
lrsVAS.MoveNext
Loop
Set lrsVAS = Nothing
And let me know.
-
Oct 24th, 2001, 07:08 AM
#7
Thread Starter
Addicted Member
Ya, its working with CLng also. Cud u explain me the reason/logic?
Thanks.
-
Oct 24th, 2001, 07:15 AM
#8
OK here goes.
The reason for your error was, that you didn't add a number ans the id for the element in the collection but rahter a reference to a class.
This is so because lrsVAS!vms_id doesn't really return a number (ie. a long) but rather a class (a reference).
When you close your DB connection and next on set the object to nothing, the reference actuallky point to Nothing, and hence you get your error.
When you include the clng() function or your "" in the end, to transform the reference to the a value that is either a long or a string.
I hope this sort of cleared things up a bit.
-
Oct 24th, 2001, 07:17 AM
#9
Thread Starter
Addicted Member
Thanks buddy. That was good enough.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|