sábado, 23 de octubre de 2010

Código fuente en VB6 para llenar un TreeView con las tablas, y otro con las vistas, de una Base de Datos en Oracle

En TreeView1 irán las tablas y en TreeView2 irán las vistas. "db" es un objeto tipo Connection, "nodx" es un objeto tipo Node, "rs" y "rs1" son Recordset, "count" es una variable tipo Integer. El estilo (propiedad Style) de los TreeViews es el número 7.

db.Open miConnectionString 'cadena de conexión a la BD en Oracle

TreeView1.Nodes.Clear
Set rs = db.OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, "TABLE"))
count = 0
Set nodx = TreeView1.Nodes.Add(, , "Root", "El título que le quiero poner")

Do Until rs.EOF
    count = count + 1
    Set nodx = TreeView1.Nodes.Add(1, tvwChild, "a" & str(count), rs!TABLE_NAME)
    rs.MoveNext
Loop
rs.Close

TreeView2.Nodes.Clear
Set rs1 = db.OpenSchema(adSchemaViews)
count = 0
Set nodx = TreeView2.Nodes.Add(, , "Root", "El título que le quiero poner")

Do Until rs1.EOF
    count = count + 1
    Set nodx = TreeView2.Nodes.Add(1, tvwChild, "a" & str(count), rs1!TABLE_NAME)
    rs1.MoveNext
Loop
rs1.Close

1 comentario:

  1. Pues bien, la duda que tengo es de como poder llenar unos datos que tengo una BD y cargarlos en un TreeView, ya eh logrado cargar los datos en el control TreeView lo que no logro es agruparlo, te muestro como me muestran los resultados en el TreeView a continuación.

    LINK: PARA VISUALIZAR LA IMAGEN
    https://s17.postimg.org/j7piqsifj/image.png

    Pues bien, como verán me cargan los datos, hasta allí todo OK, pero el detalle es de que me repite los Nro. De Ventas, aparte de eso tiene otro NODO que me muestra el detallado de dicha Venta, les muestro en la imagen siguiente:

    LINK: PARA VISUALIZAR LA IMAGEN
    https://s9.postimg.org/7gj42plbz/image.png

    Es así como me cargan los datos. Todo esta OK pero lo que trato de hacer es de que me agrupen los datos, ya se habrán dado cuenta de lo que trato de hacer, deberían de cargarme los datos de esta manera.

    - GRACIELA MORENO CALVO

    --- Numero: 3 - Fecha: 01/03/2010

    --- Cant: 1 - Cartuchos HP 1100 Remanufacturado

    --- Cant: 10 - Teclado PS2 "Genius"

    --- Numero: 7 - Fecha: 15/03/2010

    :

    :

    Quiero que me agrupe de esa manera, les dejo el código en la parte que cargo los datos, para ver en que parte tengo que agregar o midificar el codigo, gracias

    Option Explicit

    Private Enum ObjectType
    otNone = 0
    otFactory = 1
    otGroup = 2
    otPerson = 3
    otFactory2 = 4
    otGroup2 = 5
    otPerson2 = 6
    End Enum

    Private Sub Form_Load()

    CARGAR_CONSULTA_NOMBRE

    End Sub

    Sub CARGAR_VENTA_SEGUN_NOMBRE(ByVal strQuery As String)

    '+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
    '// Conectar la Base Datos con ADO
    Call ConectarBDD
    '+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+

    Dim i As Long


    Dim FACTORY As Node
    Dim GROUP As Node
    Dim ARTICULO As Node

    Dim TIPO_ORDEN As String

    StrSQL = strQuery
    RST.Open StrSQL, CN, adOpenStatic, adLockOptimistic, adCmdText

    Do While Not RST.EOF

    With TreeView1.Nodes

    .Clear
    Set FACTORY = .Add(, , , RST!NOMBRE_CLI, otFactory, 1)

    For i = 1 To RST.RecordCount

    Set GROUP = .Add(FACTORY, tvwChild, , "Número: " & RST!IDVenta & " - " & "Fecha: " & RST!Fecha_Venta, otGroup, 2)
    Set ARTICULO = .Add(GROUP, tvwChild, , "Cant.: " & RST!Cantidad & " - " & RST!Nombre, 3)

    RST.MoveNext
    FACTORY.Expanded = True

    Next i

    End With

    Loop

    '+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
    '// Cerrar la base de datos y liberar la memoria
    Call CerrarADO
    '+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+

    End Sub

    '+----------------------------------------------------------------------------------------------
    '// Consulta para mostrar los datos en el TreeView segun ID del cliente...
    '+----------------------------------------------------------------------------------------------
    Sub CARGAR_CONSULTA_NOMBRE()

    StrSQL = "SELECT VENTAS.IDVenta, VENTAS.Fecha_Venta, VENTAS_DETALLES.Cantidad, TBArticulos.Nombre, TBClientes.NOMBRE_CLI"
    StrSQL = StrSQL & " FROM (TBClientes INNER JOIN VENTAS ON TBClientes.NCLI = VENTAS.NCLI) INNER JOIN (TBArticulos INNER JOIN VENTAS_DETALLES ON TBArticulos.IDArticulo = VENTAS_DETALLES.IDArticulo) ON VENTAS.IDVenta = VENTAS_DETALLES.IDVenta"
    StrSQL = StrSQL & " WHERE ((TBClientes.NCLI=" & FrmGestionClientes.LvConsultClientes.SelectedItem & "))"

    Call CARGAR_VENTA_SEGUN_NOMBRE(StrSQL)

    End Sub

    ResponderEliminar