ACC2000: Error "Invalid Key" When Adding Node to TreeView Control (207849)



The information in this article applies to:

  • Microsoft Access 2000
  • Microsoft Office 2000 Developer

This article was previously published under Q207849
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 Office 2000 Developer software package.

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))
				

MORE INFORMATION

NOTE: The sample code in this article uses Microsoft Data Access Objects. For this code to run properly, you must reference the Microsoft DAO 3.6 Object Library. To do so, click References on the Tools menu in the Visual Basic Editor, and make sure that the Microsoft DAO 3.6 Object Library check box is selected.

Steps to Reproduce Behavior

  1. Open the sample database Northwind.mdb.
  2. Create a new form not based on any table or query.
  3. Insert a Microsoft TreeView Control, version 6.0, and name it CustOrders.
  4. Set the OnLoad property of the form to the following event procedure:
    Private Sub Form_Load()
        Dim db As DAO.DATABASE
        Dim rst As DAO.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
    					
  5. 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).

REFERENCES

For more information about the Add method of the Node object, click Microsoft Visual Basic Help on the Help menu, type Add Method (Nodes Collection) in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

For more information about the Item method, click Microsoft Visual Basic Help on the Help menu, type Item method in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

Modification Type:MajorLast Reviewed:12/12/2002
Keywords:kbcode kberrmsg kbprb KB207849 kbAudDeveloper