Results 1 to 9 of 9

Thread: Object no longer valid

  1. #1

    Thread Starter
    Addicted Member flavorjatin's Avatar
    Join Date
    Sep 2001
    Location
    India
    Posts
    154

    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.
    Best Regards.

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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]

  3. #3

    Thread Starter
    Addicted Member flavorjatin's Avatar
    Join Date
    Sep 2001
    Location
    India
    Posts
    154
    ya thats fine numeric vals can't be used as key and in my case i am using string value as key.
    Best Regards.

  4. #4
    AIS_DK
    Guest
    Show us some code, and you could properly get alot more help

  5. #5

    Thread Starter
    Addicted Member flavorjatin's Avatar
    Join Date
    Sep 2001
    Location
    India
    Posts
    154
    '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.
    Best Regards.

  6. #6
    AIS_DK
    Guest
    Could you please test this, and see if it does the trick (that is work!)

    VB Code:
    1. Do While Not lrsVAS.EOF
    2. mcolVAS.Add CLng(lrsVAS!vms_id) , UCase(lrsVAS!vms_name)
    3. lrsVAS.MoveNext
    4. Loop
    5. Set lrsVAS = Nothing

    And let me know.

  7. #7

    Thread Starter
    Addicted Member flavorjatin's Avatar
    Join Date
    Sep 2001
    Location
    India
    Posts
    154
    Ya, its working with CLng also. Cud u explain me the reason/logic?

    Thanks.
    Best Regards.

  8. #8
    AIS_DK
    Guest
    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.

  9. #9

    Thread Starter
    Addicted Member flavorjatin's Avatar
    Join Date
    Sep 2001
    Location
    India
    Posts
    154
    Thanks buddy. That was good enough.
    Best Regards.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width