Treeview population directly from MSAccess

P

Paul

Hi,
I am a self taught VBA programmer, and I'm trying to learn VB2005 Express
(The price was right).
I like the look of the treeview control, and I'd like to use it as a menu
system for my users, the options they are allowed to see are all different
and specified in a MSACCESS table.

Can I populate a Treeview directly from my MSAccess table ?

I can then of course filter the table to only show that users options.

As I am new to this I could do with some babysitting.

Thanks for any help

Paul
 
C

Cor Ligthert [MVP]

Paul,

You can build an Treeview for a AccessTable, however as a lot of people are
you probably trying to build something as your own Accesssystem with this.
Be aware that you alone can never get that functionality (and your users
expect that) as that MS access with that big Microsoft organisation has.

Just my thought,

Cor
 
P

Paul

Hi Ken,
Thanks for your response.
I checked out the code on your website and it worked great, but I need to
make my application fill the treeview from my access table.

I created and populated table with the 3 columns (ParentID,DetailId and
Description) , and created a datasource to it, then I added a datagrid to my
form from the database, so a binding source etc where all automatically
created.

Can you show me how I would now amend the code from
http://www.vb-tips.com/default.aspx?ID=4f216359-e2a6-46de-beef-04dfd3b31b2b
to fill the treeview from my table rather than from the (if I understand it
correctly) "Virtual" table that your code creates ?


If it helps, I called my table TBL_MENU.

Thanks again for your help

Paul
 
K

Ken Tucker [MVP]

Hi,

One possible solution. I used a datareader to load the treeview. I
loaded the unitprice and converted it to a decimal to format the price. You
could have used dr.getdecimal to get the price but you need to specify the
column number instead of name.

Imports System.Data.OleDb


Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim conn As OleDbConnection
Dim strConn As String
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"
strConn &= "Data Source = c:\Northwind.mdb;"


conn = New OleDbConnection(strConn)
cmd = New OleDbCommand("Select * from Products", conn)

conn.Open()
dr = cmd.ExecuteReader
Do While dr.Read
Dim nd As New TreeNode(dr.Item("ProductName").ToString)
nd.Nodes.Add(String.Format("Price {0}",
Convert.ToDecimal(dr.Item("UnitPrice")).ToString("c")))
nd.Nodes.Add(String.Format("Units In Stock {0}",
dr.Item("UnitsInStock").ToString))
nd.Nodes.Add(String.Format("Units On Order {0}",
dr.Item("UnitsOnOrder").ToString))
TreeView1.Nodes.Add(nd)
Loop
conn.Close()
End Sub
End Class


Ken
 
P

Paul

Ken,
You are my new best friend.

This works and its fantastic, Just as I was about to give in on VB and stick
with MsAccess VBA you showed me the way.

Of course this means I'll be progressing my project and asking lots more
questions !

Thanks again

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top