Treeview in Access

silentwolf

Well-known Member
Joined
May 14, 2008
Messages
1,216
Office Version
  1. 2016
Hi guys,
Just wondering if someone can tell me what I do wrong with my treeview control.
Situation:
I have a treeview control (tvwKundenRechnung) two textboxes txtKunden and txtRechnung two subforms subfrmKunde and subfrmRechnung.

CustomerId is the parent node of my treeview control. And the Invoices (Rechnungen) is the child.

When I select the invoice number the value (Rech_id) gets displayed in txtRechnung and that textbox is linked with my subfrmRechnung to show all relevant Information about the Rechnung (Invoice). That works fine!

But the problem is when I select the CustomerId in my txtKunde there is enName=101 displayed rather then only the customerId so "101"

That means that the subfrmCustomer gets displayed but with all empty fields :( ,
When I do write in the txtKunden the number 101 then the subfrmKunden displayes all relevant customer Information.

So I am wondering what the problem is and how I could fix it?

Here is the code I use and maybe someone could take a look at it for me please.

Code:
Private Sub CreateCustomerNodes()
  Dim rst As DAO.Recordset ' recordset for category data
  
  ' open the recordset for categories
  Set rst = CurrentDb.TableDefs!tblKunde.OpenRecordset
   
  ' loop through the rows in the recordset
  rst.MoveFirst
  Do Until rst.EOF
    With Me.tvwKundenRechnung.Nodes.Add(Text:=rst!Kun_id, _
      Key:="FirmenName=" & CStr(rst!Kun_id))
            .Expanded = True
    End With
          
    rst.MoveNext
  Loop
'  rst.Close
'  Set rst = Nothing
End Sub

Private Sub CreateBillNodes()
  Dim rst As DAO.Recordset ' recordset for product data

  ' open the recordset for products
  Set rst = CurrentDb.TableDefs!tblRechnung.OpenRecordset

  ' loop through the rows in the recordset
  rst.MoveFirst
  Do Until rst.EOF
    Me.tvwKundenRechnung.Nodes.Add Relationship:=tvwChild, _
        Relative:="FirmenName=" & CStr(rst!Kun_id_f), _
        Text:=rst!RechNummer, Key:="Rech=" & CStr(rst!Rech_id)
                
    rst.MoveNext
  Loop
'  rst.Close
'  Set rst = Nothing
End Sub

Private Sub cmdCollapseAll_Click()
  Dim nodthis As MSComctlLib.Node
  For Each nodthis In Me.tvwKundenRechnung.Nodes ' loop through all nodes
    nodthis.Expanded = False
  Next nodthis
  Me.tvwKundenRechnung.SetFocus
End Sub

Private Sub cmdExpandAll_Click()
  Dim nodthis As MSComctlLib.Node
  For Each nodthis In Me.tvwKundenRechnung.Nodes ' loop through all nodes
    nodthis.Expanded = True
  Next nodthis
  With Me.tvwKundenRechnung
    .SetFocus ' move focus back to the treeview

    ' make sure the selected item is back into the visible part of the treeview
    .SelectedItem.EnsureVisible
  End With
End Sub

Private Sub Form_Open(Cancel As Integer)
  SetupTreeview
  CreateCustomerNodes
  CreateBillNodes
End Sub

Private Sub SetupTreeview()
  With Me.tvwKundenRechnung
    .Style = tvwTreelinesPlusMinusText
    .LineStyle = tvwRootLines
    .Indentation = 240
    .Appearance = ccFlat
    .HideSelection = False
    .BorderStyle = ccFixedSingle
    .HotTracking = True
    .FullRowSelect = True
    .Checkboxes = False
    .SingleSel = False
    .Sorted = False
    .Scroll = True
    .LabelEdit = tvwManual
    .Font.Name = "Verdana"
    .Font.Size = 9
  End With
End Sub

Private Sub tvwKundenRechnung_Click()
    Dim nodSelected As MSComctlLib.Node ' a variable for the currently selected node
    
    Set nodSelected = Me.tvwKundenRechnung.SelectedItem ' get the currently selected node
    
    If nodSelected.Key Like "Rech=*" Then ' are we on a bill node
        Me.txtRechnung = Mid(nodSelected.Key, 6)
        Me.subfrmRechnung.Visible = True
        Me.txtKunden = Null
        Me.subfrmKunde.Visible = False
    ElseIf nodSelected.Key Like "FirmenName=*" Then ' are we on a customer node
        Me.txtKunden = Mid(nodSelected.Key, 5)
        Me.subfrmKunde.Visible = True
        Me.txtRechnung = Null
        Me.subfrmRechnung.Visible = False
    Else ' somehow this is neither a customer or bill node
        Me.txtRechnung = Null
        Me.subfrmRechnung.Visible = False
        Me.txtKunden = Null
        Me.subfrmKunde.Visible = False
    End If
    
End Sub
Would be nice if someone knows what am I doing wrong here.

Many thanks
Albert
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Albert

Do you mean 'enName=' (the node name?) is displayed as well as the customer ID?

Would the Key for this node begin with FirmenName?

Guess what Mid("FirmenName", 5) equals, it's "enName".

Perhaps it's not the Key you should use to populate txtKunden?

Or you should adjust the code to extract the customer ID.
 
Upvote 0
Hi Norie,
well I am not sure if that is what is displayed as I just started with treeview.
But yes it is enName=101 displayed instead of 101 for the customer key.
But what it means???
Well I guess I need to suptract it somehow but I am sorry I dont know how.
I did get this code of the internet and adjusted it to my needs but as you can see with not the right result :(
 
Upvote 0
Albert

I don't understand what you mean when you say you can't see what's been displayed.

It's not really the problem anyway, the problem is how the textbox is populated from the treeview.

This is the code that does that:
Code:
Me.txtKunden = Mid(nodSelected.Key, 5)
Earlier in the code, when populating the treeview, you set the key for the customer nodes with this:
Code:
Key:="FirmenName=" & CStr(rst!Kun_id))
So for a Kun_id of 101 the key for that node is 'FirmenName=101'.

So when you populate the textbox using Mid(nodSelected.Key, 5) you get 'enName=101'.

What you should be doing is using the nodes text value, which for nodes like this is the Kun_id.
Code:
txtKunden.Value = nodSelected.Text
Does that make sense?
 
Upvote 0
Hi Norie,
thanks for your help!! Much appreciated!!
Sorry yes it is what I see 'enName=101 but that is what I said no??
What I meant is I am not sure what it means.
Anyway I am sure you know what it is...
At present is treeview not making much sense to me if I am honest :(
I changed the code like this

' Me.txtKunden = Mid(nodSelected.Key, 5)
txtKunden.Value = nodSeleted.Text
but it is not working.
Sorry I just have not much knowledge about treeview and this code means not really to much for me :(
Thanks for your help!!
 
Upvote 0
Albert

What I couldn't understand is that you seemed to be saying you didn't know what was being displayed.

Anyway, that doesn't matter.

Can you not see where the 'enName=101' comes from?

The key to the node is 'KundendName=101'.

Mid("KundendName=101", 5) equals "enName=101".

As for the change not working, how is it not working?

Are you getting a compile error because you've used nodSeleted instead of nodSelected?
 
Upvote 0
Hi Norie,
you are truly a legend! It is working lol
But I am not exactly sure why well I understand what you are saying but why is still a little confusing :(
Where can I learn about treeview?
I did read up on the net but still did not find a good side for it.
How can you open a form from clicking on a node?
I am sure you know :)
Or what else can you do with a treeview?

Cheers

Albert
 
Upvote 0
Albert

Why are you using a treeview?
 
Upvote 0
Well I am still very confused I am affraid.
My database is rather complex well for me at least.
And to have it all working someone sugested to use treeview.
The problem for me is time as I am not often in the office and need to get this database somehow finished.
It is a live time experience ggg
But the most problem I have is that so many different tables are linked with other tables.
Well normal for some hard for others.
I do have problems when say tblcustomer is linked to 10 different other tables and then I used register controls
Others say it is not good to use register controls.
So I try to keep it somehow simple and userfriendly but of course functioning :)
 
Upvote 0
Albert

Not sure what you mean by registercontrols and I'm not sure why you would introduce a treeview.

It doesn't seem to be simplifying things.

If you need to be able to select a customer and display all their invoices that could be done with a simple form/subform setup.

The main form would have 2 fields the customer id and the customer name.

The sub form would be either a continuous form or a form displayed as a datasheet.

It would have all invoice records for every customer, and would include the customer id which I believe is a foreign key.

The two forms would be linked by customer id so that the sub form would only display the invoices for the customer in the main form.
 
Upvote 0

Forum statistics

Threads
1,224,606
Messages
6,179,862
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top