Treview - Move Data into an Active Treview Control

Posted on 2011-10-12
Last Modified: 2012-08-13
I want to take a tree view control on the left of a form and a data sheet on the right.  I would like to move data from the right form and put it under a specific heading on the tree view.  One value from the right could be under multiple headings on the tree.

So the perfect scenario would be for the user to highlight a value on the tree and highlight a value on the right form, push a button - and the value is still on the right but also under the heading on the Left.

Any help is greatly appreciated.

Note: The attached Treeview is Reader's Companion.mdb by Helen Feddema

Question by:LJG
    LVL 33

    Expert Comment

    Is there meant to be a file attached or do you just mean the image?

    LVL 2

    Author Comment

    Sorry - Just the image
    LVL 84
    Do you fully understand how the Treeview is populated? It's just a "tree view" of your related tables, and so all the Child Items of Greg Bear are included under  that "root" item, and so on.

    So if you wish to move an item from the Greg Bear root to the Kage Baker root, you'd have to move that data IN THE TABLES and then repopulate the treeview - assuming, of course, that you want these changes to "stick" when you close down this form.

    You can do that in several ways, but of course you'd have to have the info necessary to make those changes - for example, you'd need the PrimaryKey value for Kage Baker. Once you do this, you'd just run a simple UPDATE query, and then refresh your treeview.

    If you're talking about something else, then please explain further.
    LVL 33

    Expert Comment

    You can add items, delete items, drag and drop items in a treeview but the changes you make won't be reflected in the source data.

    So you would need to write the data back to the table either when the change is made or when you close the form.

    LVL 2

    Author Comment

    Thanks for the info

    >> My Basic Question:
     I want to do a simple add of "Merry Christmas..." under "Bisson, Terry" on the treeview without repopulating the treeview.

    >> Assumed Facts of My Process:
        1) I have a table of "tbl_Customers" (Anderson, Atwood, Baker ....) (I know in Helen's example they are authors)
        2) I have a table that is connected to customers (tbl_Purchases) that contains all the books the customer bought.
        3) By working with tbl_Customers and tbl_Purchases, I fill in the treeview control on the form.  Hence I get that Stephen Booth bought "Black Dog: A...)

    >>The Process I see
        1) The user clicks on "Bisson, Terry" in the treeview control and then clicks on "Merry Christmas..." on the right form and then clicks a button "Add"
        2) At that point I call a stored procedure that adds"Merry Christmas..." and "Bisson, Terry" to tbl_Purchases
        3) I want to do a simple add of "Merry Christmas..." under "Bisson, Terry" on the treeview without repopulating the treeview.  The treeview is not directly hooked to the tables so I'm  just am looking for a simple way to add it to the treeview.

    LVL 33

    Accepted Solution

    It's pretty easy to add to the treeview.

    This will add a child to the selected node.
    Set selNode = tvwBooks.Object.SelectedItem
    tvwBooks.Nodes.Add selNode, tvwChild, "Xmas",  "Merry Christmas"

    Open in new window

    Bad example I know but it kind of show's how it's done.

    To update the tables you would need to add a new record to tblBooks.

    Then using that ID and the author ID you need to add a record to tblBookAuthors.

    You can get the author ID from the key of the author node.

    After all that's done succesfully both table and treeview should be updated.

    The next time the treeview is populated from the table it should reflect the change(s) made.

    The whole code might look something like this.
      Dim selNode As Node
        Dim AllNodes As Nodes
        Dim NewBookID As Long
        Dim AutID As Long
        Set selNode = tvwBooks.Object.SelectedItem
        AutID = Val(Mid(selNode.Key, 9))
        tvwBooks.Nodes.Add selNode, tvwChild, "Xmas", "Merry Christmas"
        CurrentDb.Execute "INSERT INTO tblBooks(Title) VALUES('Merry Christmas')"
        NewBookID = DMax("BookID", "tblBooks")
        CurrentDb.Execute "INSERT INTO tblBookAuthors (AuthorID, BookID) VALUES (" & AutID & "," & NewBookID & ")"

    Open in new window

    LVL 2

    Author Closing Comment


    Thanks so much for the answer.  You have the points, but if you have time could you give me an explanation of the following line.

    tvwBooks.Nodes.Add selNode, tvwChild, "Xmas", "Merry Christmas"

    >>> I undersstand
        tvwBooks = Treeview Name
        Nodes = Nodes are what we see in the tree and we want to add one
        Add = we want to add a node
         "Merry Christmas" = The Value I want to add

    >>> I don't undersstand
         selNode = ??? Is this saying selected node???
         tvwChild = ??? Is this saying we are adding a child to the selected node???
          "Xmas" = ??????

    Thanks again for all the help
    LVL 33

    Expert Comment

    selNode is a variable that is a reference to the selected node in the treeview.

    Using tvwChild is to add the new child node under the selected node.

    'Xmas' is really just arbitrary - couldn't think of anything else to use at the time.

    When I think about it using the ID of the new book would probably be a better option.

    To do that the order would need to be changed a little, something like this

    1 Add book to table using SQL.

    2 Get new book's ID.

    3 Add new node, with book's ID as the Key and the book title as Text.

    The code is based on the example database so there's probably a lot of things that would need changing when
    implementing something like this in your own database.
    LVL 2

    Author Comment


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Day count current month 3 23
    Subform Link field 13 22
    Excel To Access Database Transfer 14 25
    System Analysis 5 18
    This article is a continuation or rather an extension from Cascading Combos ( and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
    Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now