Código:
Dim rs As New ADODB.Recordset Dim cn As New ADODB.Connection Private Sub Command1_Click() sSQL = "SHAPE {SELECT codcat,nomcat FROM categoria} AS CABECERA " & _ "APPEND ({SELECT codprod,nomprod,codcat FROM producto} AS DETALLE " & _ "RELATE codcat TO codcat) AS DETALLE" rs.StayInSync = False 'cn.Open "Provider=MSDataShape.1;Extended Properties=Jet OLEDB:Database Password=;Persist Security Info=False;Data Source=c:\bd_01.mdb;Data Provider=MICROSOFT.JET.OLEDB.4.0" cn.Open "Provider=MSDataShape.1;Extended Properties=Jet OLEDB:Database Password=;Persist Security Info=False;Data Source=" & App.Path & "\bd_01.mdb;Data Provider=MICROSOFT.JET.OLEDB.4.0" rs.Open sSQL, cn Set MSHFlexGrid1.DataSource = rs End Sub
Código:
Private Sub Command2_Click() Call Exportar_HFlexgrid(App.Path & "\excel1.xls", MSHFlexGrid1) End Sub ' ------------------------------------------------------------------------------------------- ' \\ -- Función para crear un nuevo libro con el contenido del Grid ' ------------------------------------------------------------------------------------------- Public Function Exportar_HFlexgrid(sOutputPath As String, FlexGrid As Object) As Boolean On Error GoTo Error_Handler Dim o_Excel As Object Dim o_Libro As Object Dim o_Hoja As Object Dim Fila As Long Dim Columna As Long ' -- Crea el objeto Excel, el objeto workBook y el objeto sheet Set o_Excel = CreateObject("Excel.Application") Set o_Libro = o_Excel.Workbooks.Add Set o_Hoja = o_Libro.Worksheets.Add ' -- Bucle para Exportar los datos With FlexGrid For Fila = 1 To .Rows - 1 For Columna = 0 To .Cols - 1 o_Hoja.Cells(Fila, Columna + 1).Value = .TextMatrix(Fila, Columna) Next Next End With o_Libro.Close True, sOutputPath ' -- Cerrar Excel o_Excel.Quit ' -- Terminar instancias Call ReleaseObjects(o_Excel, o_Libro, o_Hoja) Exportar_HFlexgrid = True Exit Function ' -- Controlador de Errores Error_Handler: ' -- Cierra la hoja y el la aplicación Excel If Not o_Libro Is Nothing Then: o_Libro.Close False If Not o_Excel Is Nothing Then: o_Excel.Quit Call ReleaseObjects(o_Excel, o_Libro, o_Hoja) If Err.Number <> 1004 Then MsgBox Err.Description, vbCritical End Function ' ------------------------------------------------------------------- ' \\ -- Eliminar objetos para liberar recursos ' ------------------------------------------------------------------- Private Sub ReleaseObjects(o_Excel As Object, o_Libro As Object, o_Hoja As Object) If Not o_Excel Is Nothing Then Set o_Excel = Nothing If Not o_Libro Is Nothing Then Set o_Libro = Nothing If Not o_Hoja Is Nothing Then Set o_Hoja = Nothing End Sub