Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble retrieving Dictionary item

Status
Not open for further replies.

ChewDoggie

Programmer
Mar 14, 2005
604
US
Hello All,

I have a Dictionary object that is Dimmed at the top of my form as "Dim DataCardCodes as Dictionary"

In the Form_Load Event, I have the following code:

Code:
    Set rsCodes = New ADODB.Recordset
    sql = "select alphacode, rid from datacards where eventid = " & EventID & " order by alphacode"
    rsCodes.open sql, cn
    Set DataCardCodes = New Dictionary
    Do While Not rsCodes.EOF
        DataCardCodes.Add Trim(rsCodes!AlphaCode), rsCodes!rid
        rsCodes.MoveNext
    Loop
    rsCodes.Close

Then in another Function in the same form, I have this code:

Code:
Private Function DataCardExists() As Boolean
    Dim id as long

    If DataCardCodes.Exists(Trim(txtCode.Text)) Then
        DataCardExists = True
        id = DataCardCodes(trim(txtCode.Text))
    Else
        DataCardExists = False
    End If
End Function

I'm just trying to retrieve the "value" (or item) of the Dictionary object. The above test passes (the code exists), but when I attempt to retrieve the value by passing in the Key, I receive an error, "Object is no longer valid".

Whaaaa???

Can anyone shed some light?

Thanks!

Chew

10% of your life is what happens to you. 90% of your life is how you deal with it.
 
OK, after searching Yahoo, I solved this one. I changed this line of code:

Code:
    Do While Not rsCodes.EOF
        DataCardCodes.Add Trim(rsCodes!AlphaCode), rsCodes!rid
        rsCodes.MoveNext
    Loop

To This:

Code:
    Do While Not rsCodes.EOF
        DataCardCodes.Add Trim(rsCodes!AlphaCode), trim(rsCodes!rid)
        rsCodes.MoveNext
    Loop

and it works fine.

Chew


10% of your life is what happens to you. 90% of your life is how you deal with it.
 
Those Trim() calls shouldn't be needed at all.

But in the case of the change you've made, what you've done is force the default property of the Field object to be invoked.

Stop writing code like:

[tt]DataCardCodes.Add rsCodes!AlphaCode, rsCodes!rid[/tt]

This should have been:

[tt]DataCardCodes.Add rsCodes!AlphaCode.Value, rsCodes!rid.Value[/tt]

... in the first place. Always be explicit about properties. There are many cases where an object is valid, and adding to a Dictionary is one example. If you want the value specify Value.

If you actually need trimming go ahead and use Trim() or better yet Trim$(), but don't program by voodoo or Cargo Cult techniques.
 

Since rid is some kind of ID, I guess, and it is probably a number in a character field (guessing again), so instead of 123 you have something like " 123". if that's the case, you may also do:

Code:
Do While Not rsCodes.EOF
    DataCardCodes.Add Trim(rsCodes!AlphaCode.Value), [blue]Val[/blue](rsCodes!rid.Value)
    rsCodes.MoveNext
Loop

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top