Microsoft KB Archive/162858

= ACC: Error "Invalid Key" When Adding Node to TreeView Control =

Article ID: 162858

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q162858



Advanced: Requires expert coding, interoperability, and multiuser skills.



SYMPTOMS
When you try to use the Add method of the Node object with the TreeView control, you may receive the following error message:

Run-time error '35603':

Invalid key

NOTE: The TreeView control is only available if you have installed the Microsoft Access Developer's Toolkit version 7.0 or the Office 97 Developer Edition.



CAUSE
The Item Method, which can be used to return a specific member of the Nodes collection, takes one argument. If this argument is a numeric expression, the Item method searches for the node by index; if the argument is a string expression, the Item method searches for the node by key. Because this argument is a Variant, the Item method cannot distinguish a key expression from an index expression unless that expression contains at least one non-numeric character.



RESOLUTION
Concatenate at least one non-numeric character to the expression in the key argument of the Add method. For example, add a node by using

  Set Node = Me!CustOrders.Nodes.Add(,, "Node " & rst!OrderID,   CStr(rst!OrderID))

instead of

  Set Node = Me!CustOrders.Nodes.Add(,, rst!OrderID, CStr(rst!OrderID))



Steps to Reproduce Behavior
 Open the sample database Northwind.mdb. Create a new form not based on any table or query. Insert a TreeView control and name it CustOrders.  Set the form's OnLoad property to the following event procedure:

Private Sub Form_Load Dim db As DATABASE, rst As Recordset Dim Node As Object Set db = CurrentDb Set rst = db.OpenRecordset("Orders", dbOpenDynaset) If rst.RecordCount > 0 Then Do Until rst.EOF Set Node = Me!CustOrders.Nodes.Add(,, _                        rst!OrderID,cstr(rst!OrderID)) rst.MoveNext Loop End If        rst.Close db.Close End Sub </li> Open the form in Form View. Note that you receive the following error message:

Run-time error '35603': Invalid Key

The error occurs even if you use the Cstr function with the key argument (the third argument of the Add method).</li></ol>

<div class="references_section">