Solved

Populate Combo box from treeview

Posted on 2011-09-25
9
642 Views
Last Modified: 2012-05-12
I have a treeview and it works just fine. Now, what I'm trying to do is use the information in the treeview to populate a combo box. For example, if a user wants to add a new category, I want them to enter the information in a pop-up form and if the new category is a sub-category of an existing category, then I want them to be able to select the parent category from the combo box. This logic will also be used to change a category or sub-category. The thing about it is, I'm not sure where to begin because the combo box needs to be populated like this:
Category1
   sub1
   sub2
        sub2-sub1
        sub2-sub2
Category 2
...and so on

The categories should be lined up under each other (indented as appropriate) after a change the treeview need to be refreshed.
0
Comment
Question by:stevensont
9 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 36596431
Isn't that kind of populating the combobox just like the treeview?

0
 
LVL 61

Expert Comment

by:mbizup
ID: 36596459
While treeview controls are designed to show that kind of dependent relationship in a single control, combo boxes really are not - and formatting within a drop-down list can be cumbersome.

When using combo boxes, dependent data is generally shown with multiple "cascading" combo boxes.  For example, the user make a selection of "Category 1" in the first combo box, and that selection limits the options in combo2 to only those that pertain to Category 1.

Here's a tutorial:
http://www.blueclaw-db.com/comboboxlist/access_dependent_dropdown_list.htm
0
 
LVL 33

Expert Comment

by:Norie
ID: 36596509
This code will populate a listbox with the child nodes of the node you click in a treeview.

Also if you click an item (which is sort of node) in the listbox then the listbox will be populated with the children of that item.

Note, it's pretty rough code but works for me with a simple treeview.
Private Sub ListBox1_Click()
Dim ndList As Node
Dim ndChild As Node

    If ListBox1.ListIndex <> -1 Then
        Set ndList = TreeView1.Nodes(Val(ListBox1.List(ListBox1.ListIndex, 1)))

        Set ndChild = ndList.Child
        If Not ndChild Is Nothing Then

            ListBox1.Clear
            Do

                ListBox1.AddItem ndChild.Text
                ListBox1.List(ListBox1.ListCount - 1, 1) = ndChild.Index
                Set ndChild = ndChild.Next

            Loop Until ndChild Is Nothing

        End If
    End If

End Sub

Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
Dim ndChild As Node

    Set ndChild = Node.Child
    If Not ndChild Is Nothing Then

        ListBox1.Clear
        Do

            ListBox1.AddItem ndChild.Text
            ListBox1.List(ListBox1.ListCount - 1, 1) = ndChild.Index
            Set ndChild = ndChild.Next

        Loop Until ndChild Is Nothing

    End If
End Sub

Open in new window

0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 21
ID: 36596612
I see more and  more web apps (shopping carts, CMS, blogs, etc)  using a combo box to do this. I also do it in Access.

 combo box example
0
 
LVL 33

Expert Comment

by:Norie
ID: 36596663
Looks quite good but wouldn't it be tricky getting the indentation etc right.

0
 

Author Comment

by:stevensont
ID: 36596673
Hey TheHiTechCoach, that is exactly what I'm trying to do in an Access combo box. I agree with Imnorie, how to get the indentations etc right. You have an example of code that does that from a treeview?
0
 
LVL 33

Expert Comment

by:Norie
ID: 36596687
Oops, just realised the code I posted wasn't for Access.

It should still work though.
0
 

Accepted Solution

by:
stevensont earned 0 total points
ID: 36596996
Well experts, here I found my answer. The author has great examples and explanations that I was quickly able to incorporate into my DB and it works just like described.
0
 

Author Closing Comment

by:stevensont
ID: 36890360
close.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

837 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