Urgent Solution to treeview

Imports MSComctlLib
Imports System
Imports System.Data.OleDb
Imports ADODB
Public Class Form1


Dim cn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim rs As ADODB.Recordset
Dim MyDataFile As String

Dim dbNode As Node
Dim tabNode As Node
Dim fldNameNode As Node
Dim ColHeader() As String
Dim ColHeaders As String

Private Sub cmdLoadData_Click()

Dim strCon As String
Dim tabName As String
Dim fldName As String
Dim n As Integer
Dim c As Integer
Dim i As Integer
Dim j As Integer

On Error Resume Next

MyDataFile = "D:Documents and SettingsyiacovidesDesktop" & "final.mdb"

strCon = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & MyDataFile & ";"

lblDatabase.Text = MyDataFile

'init TreeView
tvdata.Nodes.Clear()

cn = New ADODB.Connection
cn.CursorLocation = ADODB.CursorLocationEnum.adUseClient
cn.Open(strCon)

Cursor = Cursors.WaitCursor

' set root node of TreeView.
dbNode = tvdata.Nodes.Add(, , "RootDB", "Database", "DatabaseClose")
dbNode.Tag = "RootDB"


'rst = cn.OpenSchema(adSchemaTables)
rst = cn.OpenSchema(ADODB.SchemaEnum.adSchemaTables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
rst.MoveFirst()

' set table nodes:
Do Until rst.EOF
If rst("TABLE_TYPE").Value = "VIEW" Or rst("TABLE_TYPE").Value = "TABLE" Then
tabName = rst("TABLE_NAME").Value
tabNode = tvdata.Nodes.Add("RootDB", [b]tvwChild[/b][color=Red][/color], "X" & tabName, tabName, "TableClose")
tabNode.Tag = "Tables"
End If
rst.MoveNext()
Loop

rst.Close()

If tabNode Is Nothing Then
Cursor = Cursors.Default
Exit Sub
End If

rs = New ADODB.Recordset

c = tvdata.Nodes("RootDB").Children
' first table
n = tvdata.Nodes("RootDB").Child.FirstSibling.Index

For i = n To c + n
tabName = tvdata.Nodes(i).Text
' get field name
rs = cn.Execute("SELECT * FROM [" & tabName & "]", 1, 1)
For j = 0 To rs.Fields.Count - 1
fldName = rs.Fields(j).Name
fldNameNode = tvdata.Nodes.Add("X" & tabName, [b]tvwChild,[/b][color=Red][/color] "X" & fldName, fldName, "imgField")
fldNameNode.Tag = "Fields"
Next j
rs.Close()
Next i

' sort top nodes:
tvdata.Nodes(1).Sorted = True
' collapse top node:
tvdata.Nodes(1).Expanded = False

Cursor = Cursors.Default

End Sub



Private Sub tvData_Expand(ByVal Node As Node)

If Node.Tag = "RootDB" Then
Node.Image = "DatabaseOpen"
ElseIf Node.Tag = "Tables" Then
Node.Image = "TableOpen"
End If

End Sub



Private Sub tvData_Collapse(ByVal Node As Node)

If Node.Tag = "RootDB" Then
Node.Image = "DatabaseClose"
ElseIf Node.Tag = "Tables" Then
Node.Image = "TableClose"
End If

End Sub



Private Sub tvData_NodeClick(ByVal Node As MSComctlLib.Node)

Dim nodTag As String
Dim tabName As String
Dim n As Integer


nodTag = Node.Tag

If nodTag = "Tables" Then
'if no Fields in Table
If Node.Children = 0 Then Exit Sub
'read Table name
tabName = Node.Text
'read Fields name
n = Node.Child.FirstSibling.Index
ColHeaders = Node.Child.FirstSibling.Text

Do Until n = Node.Child.LastSibling.Index
' string includes all Fields name
ColHeaders = ColHeaders & ";" & tvdata.Nodes(n).Next.Text
n = tvdata.Nodes(n).Next.Index
Loop

End If


End Sub

End Class


I did not find a solution so far,
any help would be appriciated. I am using
VB studio 2008 in XP operating system


It sais the tvwchild is not declared






Comments

  • seancampbellseancampbell Pennsylvania, USA
    The program is saying Tvwchild is not declared because it does not recognize that one ever has been declared.

    Is this supposed to be an object that is declared in the front end, or is it supposed to represent a Certain child record inside of your TreeView? I don't know what you are expecting for that variable...

    In Visual Basic .Net 3.5, TreeView.Nodes.Add doesn't have a version that accepts 5 inputs, I only see one with 4 string parameters (and others with less) so I am assuming that this involves some non-standard treeview? Or some custom code atleast?

    Need more information to help you more:
    Sean C
  • Thank you for your fast responce

    In order to give you a clear view of I am trying to do,
    I' am attaching the working code I have on VB6 and works just fine.

    The working code is:

    Option Explicit

    Dim cn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim rs As ADODB.Recordset
    Dim MyDataFile As String

    Dim dbNode As Node
    Dim tabNode As Node
    Dim fldNameNode As Node
    Dim ColHeader() As String
    Dim ColHeaders As String

    Private Sub cmdLoadData_Click()

    Dim strCon As String
    Dim tabName As String
    Dim fldName As String
    Dim n As Integer
    Dim c As Integer
    Dim i As Integer
    Dim j As Integer

    On Error Resume Next

    MyDataFile = "D:Documents and SettingsyiacovidesDesktop" & "final.mdb"

    strCon = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & MyDataFile & ";"

    lblDatabase = MyDataFile

    'init TreeView
    tvData.Nodes.Clear

    Set cn = New ADODB.Connection
    cn.CursorLocation = adUseClient
    cn.Open strCon

    Screen.MousePointer = vbHourglass

    ' set root node of TreeView.
    Set dbNode = tvData.Nodes.Add(, , "RootDB", "Database", "DatabaseClose")
    dbNode.Tag = "RootDB"

    Set rst = cn.OpenSchema(adSchemaTables)
    rst.MoveFirst

    ' set table nodes:
    Do Until rst.EOF
    If rst("TABLE_TYPE").Value = "VIEW" Or rst("TABLE_TYPE").Value = "TABLE" Then
    tabName = rst("TABLE_NAME").Value
    Set tabNode = tvData.Nodes.Add("RootDB", tvwChild, "X" & tabName, tabName, "TableClose")
    tabNode.Tag = "Tables"
    End If
    rst.MoveNext
    Loop

    rst.Close

    If tabNode Is Nothing Then
    Screen.MousePointer = vbDefault
    Exit Sub
    End If

    Set rs = New ADODB.Recordset

    c = tvData.Nodes("RootDB").Children
    ' first table
    n = tvData.Nodes("RootDB").Child.FirstSibling.Index

    For i = n To c + n
    tabName = tvData.Nodes(i).Text
    ' get field name
    Set rs = cn.Execute("SELECT * FROM [" & tabName & "]", 1, 1)
    For j = 0 To rs.Fields.Count - 1
    fldName = rs.Fields(j).Name
    Set fldNameNode = tvData.Nodes.Add("X" & tabName, tvwChild, "X" & fldName, fldName, "imgField")
    fldNameNode.Tag = "Fields"
    Next j
    rs.Close
    Next i

    ' sort top nodes:
    tvData.Nodes(1).Sorted = True
    ' collapse top node:
    tvData.Nodes(1).Expanded = False

    Screen.MousePointer = vbDefault

    End Sub
    Private Sub Form_Unload(Cancel As Integer)

    If Not rs Is Nothing Then
    If rs.State = adStateOpen Then rs.Close
    End If
    Set rs = Nothing

    If Not rst Is Nothing Then
    If rst.State = adStateOpen Then rst.Close
    End If
    Set rst = Nothing

    If Not cn Is Nothing Then
    If cn.State = adStateOpen Then cn.Close
    End If
    Set cn = Nothing

    End Sub


    Private Sub tvData_Expand(ByVal Node As Node)

    If Node.Tag = "RootDB" Then
    Node.Image = "DatabaseOpen"
    ElseIf Node.Tag = "Tables" Then
    Node.Image = "TableOpen"
    End If

    End Sub



    Private Sub tvData_Collapse(ByVal Node As Node)

    If Node.Tag = "RootDB" Then
    Node.Image = "DatabaseClose"
    ElseIf Node.Tag = "Tables" Then
    Node.Image = "TableClose"
    End If

    End Sub

    Private Sub cmdClose_Click()

    Unload Me

    End Sub

    '''''''''''''''''''''''''''''''''''

    What I want to do is, after a user create a database in access for example and select it in the form I have created, the database to be populated in a treeview.

    Do you have any idea how I can make it on vb.net?




  • seancampbellseancampbell Pennsylvania, USA
    Using a TreeView in .Net is only slightly different than VB6, as is connecting to and retreiving data from an Access Database.

    I have not used the TreeView extensively, and could not convert your code for you (actually I could, but it would take me at least 2 hours and I'm not really getting payed to do this). However, if the VB6 code works, converting it to VB.Net should be very simple as the languages are very very similar...

    Here is something that may help:
    http://www.a1vbcode.com/app.asp?ID=2902 - Download the program on this page, it is an example of a program that populates a TreeView from an Access 2000 database.

    http://dotnet.sys-con.com/node/46335 - A good article on converting from VB6 to VB.Net

    Hope that helps, if you have problem converting specific lines or errors in the code you create, post it up in a new thread and I will gladly help!!
    Sean Campbell
  • Thank you very much for help, I have allready tryed these but the error exist just with the tvwchild.

    Thank you very much for your responses.
  • seancampbellseancampbell Pennsylvania, USA
    tvData.Nodes.Add("RootDB", tvwChild, "X" & tabName, tabName, "TableClose")

    There are too many parameters passed to this function. If you change tvwChild to a String like "tvwChild", then the whole line will be underlined blue... I suggest trying this:

    Set tabNode = tvData.Nodes.Add("RootDB", "X" & tabName, tabName, "TableClose")

    Just removing tvwChild all together, since your code doesn't reference that name anywhere else, I assume that it is an artifact from you converting from VB6
  • Thanks a lot,

    I'll try it now,

    I was trying to replay yesterday but something was wrong.

    Thanks again
  • Hi, I don't know if you got my replay earlier, thanks very much for your respond.

    I have removed tvwchild as you said,"no errors shows any more", but just when i press the load button i get those messages and crash.

    Thanks again,
    I appriciated your help
    ''''''''''''''''''''''''''''''''''

    A first chance exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe

    A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in WindowsApplication1.exe
  • seancampbellseancampbell Pennsylvania, USA
    I need to know exactly what line it crashed on.

    Make your software crash, click "Details" on the error message, copy it's content and post it here, we will go from there. There is a bunch of information in there that will help us determine exactly what is hapenning
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories