Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

Excel chart OLE problem

I want to draw a chart by using ole and excel chart, the
data value to be plotted are in a dbgrid
does anyone know how to use these values in dbgrid and
draw a chart...I don't want to go to excel sheet.. but
from dbgrid in Vb to excel chart...........

0
zakman
Asked:
zakman
1 Solution
 
zakmanAuthor Commented:
Edited text of question
0
 
rantanenCommented:
zakman,

here's a sub which takes a column in DBGrid and makes an Excel bar chart of the data

Private Sub MakeBarChart(nDBGridCol As Integer, nExcelCol As Integer)
    Dim objXLsheet As Object
    Dim objRange As Object
    Dim objChart As Object

    Dim nPoints As Integer

    Form1.MousePointer = vbHourglass
     
    ' Create a Worksheet Object:
    Set objXLsheet = CreateObject("Excel.Sheet")

    ' Insert data into an Excel column from a DBGrid's column
    nExcelCol = 1
    nDBGridCol = 1
    With Form1.DBGrid1
        .Col = nDBGridCol
        Data1.Recordset.MoveFirst
        nPoints = 1
        While Not Data1.Recordset.EOF
            objXLsheet.Cells(nPoints, nExcelCol).Value = CInt(DBGrid1.Text)
            Data1.Recordset.MoveNext
            nPoints = nPoints + 1
        Wend
    End With
           
    ' Add a ChartObject to the worksheet:
    Set objChart = objXLsheet.ChartObjects.Add(100, 100, 200, 200)

    ' Assign the column created above as a series for the chart:
    objChart.Chart.SeriesCollection.Add objXLsheet.Columns(nExcelCol), xlColumns

    ' Make Excel Visible:
    objXLsheet.Application.Visible = True
    DoEvents

    ' Save the Worksheet to disk.
    objXLsheet.Parent.SaveAs "C:\XLCHART.XLS"

    ' Close this instance of Excel:
    objXLsheet.Application.Quit
    MousePointer = vbArrow
End Sub

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now