+ Reply to Thread
Results 1 to 17 of 17

Treeview in VBA

  1. #1
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Treeview in VBA

    Hey All,

    I am using a treeview in my project. When I add a parent, I see it popping up, but when I try to add a child, nothing appears under the parent in the treeview...
    Does anyone know what is going on?

    My code to create the parent and the child nodes:

    Please Login or Register  to view this content.
    If anyone could think of a fix, I would be super happy!

    Thanks in advance and cheers,

    Devatu

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,258

    Re: Treeview in VBA

    You should be adding to the Nodes collection of the parent node, not the treeview.
    Remember what the dormouse said
    Feed your head

  3. #3
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: Treeview in VBA

    Hey romperstomper, thanks for your quick reply!

    I've Followed the tutorial here http://www.homeandlearn.org/parent_and_child_nodes.html
    They have posted a picture of it working in that way.

    But I believe you, how should I add to the parent node instead of the treeview itself? And why doesn't it work like in the tutorial stated above?

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Treeview in VBA

    You aren't specifiying the parent node.
    Please Login or Register  to view this content.
    PS Your code actually works, try expanding Parent 1.
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: Treeview in VBA

    Hey Norie, thanks for the reply.

    I've added your code and it still gives the same result:
    See.png

    Is it my computer?

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Treeview in VBA

    Have you tried expanding the parent node by double clicking it?

    Or by adding this code.
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: Treeview in VBA

    Yeah, I just checked it on another computer. It wasn't expanded indeed, but I never checked it because I expected to have a + sign in front of it. Sorry for being stupid haha! And thanks for the help!

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Treeview in VBA

    To get the + sign(s) you need to set LineStyle to tvwRootLines.

  9. #9
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: Treeview in VBA

    Thanks Norie! Couldn't find that option either, but thanks to you I found it !

  10. #10
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: Treeview in VBA

    Thanks again, is there a way to detect if a node is a parent or not? I want to add a child to a parent, but at the moment I am able to add a child to a child aswell.
    This is due to the dynamically selection procedure I am using (the objNode.text one returns the selected item). If I would be able to check whether something is a parent or a child, I could change this.

    My code is as following:
    Please Login or Register  to view this content.
    Cheers and Thanks in advance!

    Devatu

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Treeview in VBA

    I suppose you could check the no of children a node has using it's Children property, if it's 0 then it's a child node.

  12. #12
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: Treeview in VBA

    I can indeed count the amount of childs/children with it, but when I have no childs/children, I get an error that the With isn't set. I tried to do:

    Please Login or Register  to view this content.
    But then it gives the with error in that part of the code

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Treeview in VBA

    When you run that code is there a node selected?

  14. #14
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: Treeview in VBA

    Oh, that is stupid of me. Nothing was selected indeed. It works now, but when nothing is selected I get the error (because it has nothing selected, I learned that now), but I don't want the end-user to get an error message if he didn't select anything. Is there a way to overcome this? I know that with listboxes, if you have nothing selected , you can use the .selected (or something like that) function to check if it's value is -1. Is there something equal possible here? thanks

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Treeview in VBA

    With comboboxes and single select listboxes you can check ListIndex to see if anything is selected.

    With a treeview if nothing is selected SelectedItem is Nothing.
    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: Treeview in VBA

    That works fine indeed, thanks.

    Just one question remaining. With the given code for checking whether the node is a child node or not.
    If the parent node has no childs, the code will return a 0, because it has no childs. So I press my button to add a child, and it works, because he had none. But then, because the code is dynamically, if I press the button again, the child has no childs, so my program makes another child.

    Please Login or Register  to view this content.
    So I still cannot determine whether something is a parent or not.
    Last edited by devatu; 08-25-2015 at 07:30 AM.

  17. #17
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: Treeview in VBA

    Solved it by using:

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. TreeView Help
    By texanjohn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-21-2009, 03:05 PM
  2. using a treeview
    By dadio25 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 03-18-2009, 12:07 PM
  3. Treeview Question
    By vqthomf in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-30-2006, 07:55 AM
  4. Treeview on a sheet
    By PatrickS in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-03-2006, 04:40 AM
  5. How to use TreeView control
    By Dennis in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-01-2006, 02:40 PM
  6. [SOLVED] TreeView
    By Stephen in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-10-2005, 02:05 PM
  7. TreeView Help
    By Tony in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-21-2005, 01:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1