Little confused

Can someone please explain to me why this piece of
code works differently if its in another form, compared
to if it's in a button.

// PushButton code
  TTreeNode *ANode = TreeView1->Selected;
    PUserNodeData UserData = ((PUserNodeData)ANode->Data);

    if( !UserData )
    {
        UserData = new TUserNodeData;
        ANode->Data = UserData;
    }
        UserData->Account = Edit1->Text;
        UserData->Name = Edit2->Text;
        Modified = true;

It stores the information correctly if i put the code in a button on the
main form.

If i try it like this it does nothing with the information thats entered.

    TTreeNode *ANode = TreeView1->Selected;   // Add a child node
    if( ANode && ANode->Level < 1 )
    {
        ANode = TreeView1->Items->AddChild( ANode, "New Item" );
        TreeView1->SetFocus();
        TreeView1->Selected = ANode;
        TreeView1->Selected->MakeVisible();
        Edit1->SetFocus();

        Form2->ShowModal();

        if( Form2->ModalResult == mrOk )
        {
            // all the code formerly in the OK button handler
         //   TTreeNode *ANode = TreeView->Selected;
            PUserNodeData UserData = ((PUserNodeData)ANode->Data);

            if( !UserData )
            {
                UserData = new TUserNodeData;
                ANode->Data = UserData;
            }
                UserData->Account = Form2->Edit1->Text;
                UserData->Name = Form2->Edit2->Text;
                Modified = true;
        }
    }

It goes through the steps like it working, but only the text for the node is
saved on
the treeview, none of the data for the edit fields are retained at all.
If i put the pushbutton code above into a button on Form2 it still does not
work.

Thanks....