Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse DAO to display a database's structure in a TreeView control
DescriptionThis example shows how to use DAO to display a database's structure in a TreeView control in Visual Basic 6.
Keywordsdatabase, DAO, structure, TreeView
CategoriesDatabase, Controls
 
This example opens the database and uses its TableDefs collection to get information about the tables. It uses TableDef object's Fields and Indexes collections to get information about the table's fields and indexes.
 
Private Sub cmdOpen_Click()
Dim db As DAO.Database
Dim table_def As TableDef
Dim table_node As Node
Dim field_node As Node
Dim index_node As Node
Dim field_obj As Field
Dim index_obj As Index
Dim txt As String

    ' Open the database.
    Set db = _
        DAO.DBEngine(0).OpenDatabase(txtDatabaseName.Text)

    ' Clear the tree.
    If trvDatabase.Nodes.Count > 0 Then
        trvDatabase.Nodes(0).Expanded = False
        trvDatabase.Nodes.Clear
    End If

    ' Get Table information.
    For Each table_def In db.TableDefs
        ' Display the table's information.
        Set table_node = trvDatabase.Nodes.Add( _
            Text:=table_def.Name)

        ' Display field information.
        Set field_node = trvDatabase.Nodes.Add( _
            Relative:=table_node, _
            Relationship:=tvwChild, _
            Text:="Fields")
        For Each field_obj In table_def.Fields
            trvDatabase.Nodes.Add _
                Relative:=field_node, _
                Relationship:=tvwChild, _
                Text:=field_obj.Name & _
                    " (" & DbTypeName(field_obj.Type) & ")"
        Next field_obj

        ' Display index information.
        Set index_node = trvDatabase.Nodes.Add( _
            Relative:=table_node, _
            Relationship:=tvwChild, _
            Text:="Indexes")

        ' We will not have permission to read index
        ' information on the system tables.
        On Error Resume Next
        For Each index_obj In table_def.Indexes
            txt = ""
            For Each field_obj In index_obj.Fields
                txt = txt & ", " & field_obj.Name
            Next field_obj
            If Len(txt) > 0 Then txt = Mid$(txt, 3)
            trvDatabase.Nodes.Add _
                Relative:=index_node, _
                Relationship:=tvwChild, _
                Text:=index_obj.Name & _
                    " (" & txt & ")"
        Next index_obj
        On Error GoTo 0
    Next table_def

    db.Close
End Sub
 
The handy helper function DbTypeName returns the data type name for a numeric database type value.
 
Private Function DbTypeName(ByVal type_value As Integer) As _
    String
    Select Case type_value
        Case 16
            DbTypeName = "BigInt"
        Case 9
            DbTypeName = "Binary"
        Case 1
            DbTypeName = "Boolean"
        Case 2
            DbTypeName = "Byte"
        Case 18
            DbTypeName = "Char"
        Case 5
            DbTypeName = "Currency"
        Case 8
            DbTypeName = "Date"
        Case 20
            DbTypeName = "Decimal"
        Case 7
            DbTypeName = "Double"
        Case 15
            DbTypeName = "GUID"
        Case 3
            DbTypeName = "Integer"
        Case 4
            DbTypeName = "Long"
        Case 11
            DbTypeName = "LongBinary"
        Case 12
            DbTypeName = "Memo"
        Case 19
            DbTypeName = "Numeric"
        Case 6
            DbTypeName = "Single"
        Case 10
            DbTypeName = "Text"
        Case 22
            DbTypeName = "Time"
        Case 23
            DbTypeName = "TimeStamp"
        Case 17
            DbTypeName = "VarBinary"
        Case Else
            DbTypeName = "Unknown"
    End Select
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated