• Sábado 4 de Mayo de 2024, 00:09

Autor Tema:  Duda Visual Basic, Access Y Excel  (Leído 4566 veces)

OhcanBSK

  • Nuevo Miembro
  • *
  • Mensajes: 4
    • Ver Perfil
Duda Visual Basic, Access Y Excel
« en: Miércoles 18 de Abril de 2007, 12:07 »
0
Hola!
Alguien sabe cómo (en código Visual Basic) exportar la tabla de una consulta access a un documento excel para trabajar con ella??
Gracias por adelantado!

F_Tanori

  • Moderador
  • ******
  • Mensajes: 1919
  • Nacionalidad: mx
    • Ver Perfil
Re: Duda Visual Basic, Access Y Excel
« Respuesta #1 en: Miércoles 18 de Abril de 2007, 12:19 »
0
Te refieres desde Access (VBA) ?

Código: Text
  1.  
  2. Sub ExportarTablaExcel(sTabla As String, sRutaArchivoExcel, IncluirCamos As Boolean)
  3. On Error GoTo xError
  4.  
  5.     DoCmd.TransferSpreadsheet acExport, 8, sTabla, sRutaArchivoExcel, IncluirCamos, ""
  6.     Exit Sub
  7.  
  8. xError:
  9.     MsgBox Err.Description
  10. End Sub
  11.  



Saludos
" ExIsTo y A vEcEs PiEnSo "

NOTA:
===========================================================================================================================
Este foro es para ayudar, aprender, compartir... usenlo para eso,
NO SE RESUELVEN DUDAS POR MENSAJE PRIVADO Y MENOS POR CORREO
===========================================================================================================================

OhcanBSK

  • Nuevo Miembro
  • *
  • Mensajes: 4
    • Ver Perfil
Re: Duda Visual Basic, Access Y Excel
« Respuesta #2 en: Miércoles 18 de Abril de 2007, 12:50 »
0
Funciona perfectamente. Muchas gracias.
Saludos!

OhcanBSK

  • Nuevo Miembro
  • *
  • Mensajes: 4
    • Ver Perfil
Re: Duda Visual Basic, Access Y Excel
« Respuesta #3 en: Jueves 19 de Abril de 2007, 10:24 »
0
Hola otra vez,
Aqui os dejo el código de mi función, que coge la tabla de una consulta en access y la pasa a excel creando un gráfico de la misma pero no tira, a ver si podeis ver donde está el fallo o si teneis un algoritmo mejor... Gracias!

Código: Text
  1.  
  2. Function CreateChart(strSourceName As String, strFileName As String)
  3.  
  4.    Dim xlApp As Excel.Application
  5.    Dim xlWrkbk As Excel.Workbook
  6.    Dim xlChartObj As Excel.Chart
  7.    Dim xlSourceRange As Excel.Range
  8.    Dim xlColPoint As Excel.Point
  9.  
  10.    On Error GoTo Err_CreateChart
  11.  
  12.    ' Create an Excel workbook file based on the
  13.    ' object specified in the second argument.
  14.    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel97, strSourceName, strFileName, False
  15.    ' Create a Microsoft Excel object.
  16.    Set xlApp = CreateObject("Excel.Application")
  17.    ' Open the spreadsheet to which you exported the data.
  18.    Set xlWrkbk = xlApp.Workbooks.Open(strFileName)
  19.    ' Determine the size of the range and store it.
  20.    Set xlSourceRange = xlWrkbk.Worksheets(1).Range("a1").CurrentRegion
  21.    ' Create a new chart.
  22.    Set xlChartObj = xlApp.Charts.Add
  23.    ' Format the chart.
  24.    With xlChartObj
  25.  
  26.       ' Specify chart type as 3D.
  27.       .ChartType = xl3DColumn
  28.       ' Set the range of the chart.
  29.       .SetSourceData Source:=xlSourceRange, PlotBy:=xlColumns
  30.       ' Specify that the chart is located on a new sheet.
  31.       .Location Where:=xlLocationAsNewSheet
  32.  
  33.       ' Create and set the title; set title font.
  34.       .HasTitle = True
  35.       'With .ChartTitle
  36.          .Characters.Text = "Total Sales by Country"
  37.          .Font.Size = 18
  38.       End With
  39.  
  40.       ' Rotate the x-axis labels to a 45-degree angle.
  41.       .Axes(xlCategory).TickLabels.Orientation = 45
  42.       ' Delete the label at the far right of the x-axis.
  43.       .Axes(xlSeries).Delete
  44.       ' Delete the legend.
  45.       .HasLegend = True
  46.  
  47.       ' Set each datapoint to show the dollar amount
  48.       ' and format the datapoint to be currency
  49.       ' with no decimals.
  50.       With .SeriesCollection(1)
  51.          .ApplyDataLabels Type:=xlDataLabelsShowValue
  52.          .DataLabels.NumberFormat = "$#,##0"
  53.       End With
  54.  
  55.    End With
  56.  
  57.    ' Position the points further from the tops
  58.    ' of the columns.
  59.    For Each xlColPoint In xlChartObj.SeriesCollection(1).Points
  60.       xlColPoint.DataLabel.Top = xlColPoint.DataLabel.Top - 11
  61.    Next xlColPoint
  62.  
  63.    ' Save and close the workbook
  64.    ' and quit Microsoft Excel.
  65.    With xlWrkbk
  66.       .Save
  67.       .Close
  68.    End With
  69.  
  70.    xlApp.Quit
  71.  
  72. Exit_CreateChart:
  73.    Set xlSourceRange = Nothing
  74.    Set xlColPoint = Nothing
  75.    Set xlChartObj = Nothing
  76.    Set xlWrkbk = Nothing
  77.    Set xlApp = Nothing
  78.    Exit Function
  79.  
  80. Err_CreateChart:
  81.  
  82.    MsgBox CStr(Err) & " " & Err.Description
  83.    Resume Exit_CreateChart
  84.  
  85. End Function
  86.  
  87.  

OhcanBSK

  • Nuevo Miembro
  • *
  • Mensajes: 4
    • Ver Perfil
Re: Duda Visual Basic, Access Y Excel
« Respuesta #4 en: Jueves 19 de Abril de 2007, 12:13 »
0
El error puede ser por no tener habilitada una libreria en concreto pero no se cual. ¿Cúal es la libreria para poder "pintar" graficos en Excel?

F_Tanori

  • Moderador
  • ******
  • Mensajes: 1919
  • Nacionalidad: mx
    • Ver Perfil
Re: Duda Visual Basic, Access Y Excel
« Respuesta #5 en: Viernes 20 de Abril de 2007, 09:44 »
0
Ocupas las referencias del excel y graph





Dependiendo de la version que utlices



quitale el comentario (') a la linea

Código: Text
  1.  'With .ChartTitle
  2.  

Saludos
" ExIsTo y A vEcEs PiEnSo "

NOTA:
===========================================================================================================================
Este foro es para ayudar, aprender, compartir... usenlo para eso,
NO SE RESUELVEN DUDAS POR MENSAJE PRIVADO Y MENOS POR CORREO
===========================================================================================================================