Hi all,
I'm trying to format an Excel chart from an Access module. My goal is to have a temporary Excel workbook that appears to show data, and I don't want to have another Excel file as a template if at all possible.
As of now I can successfully add all of the required data to the excel workbook and create the chart as the default bar graph. The only problem is that I can't format the chart to change any of the properties. I recorded a macro from Excel with the changes I wanted to make to the format, but Access VBA doesn't recognize the properties. For example it does not recognize xlXYScatterLines for chart type or xlBottom for legend position even when it's taken directly from the recorded Excel macro. Is there a difference between Access and Excel VBA? Is there different syntax/statements I should be using to manipulate the Excel graph from Access?
I have these references selected: Excel 15.0 Object Library, Microsoft Graph 15.0 Object Library, Microsoft Office 15.0 Object Library, as well as the defaults. Is there any other references that should be added?
The frustrating part is it was working fine on another computer with the same versions of Office (2013), but after switching now it won't work :banghead:
I have it set up as late binding, also tried early binding but to no avail. I'm hoping I'm just missing something minor that I haven't flushed out yet.
Any help would greatly appreciated
-Pat
I'm trying to format an Excel chart from an Access module. My goal is to have a temporary Excel workbook that appears to show data, and I don't want to have another Excel file as a template if at all possible.
As of now I can successfully add all of the required data to the excel workbook and create the chart as the default bar graph. The only problem is that I can't format the chart to change any of the properties. I recorded a macro from Excel with the changes I wanted to make to the format, but Access VBA doesn't recognize the properties. For example it does not recognize xlXYScatterLines for chart type or xlBottom for legend position even when it's taken directly from the recorded Excel macro. Is there a difference between Access and Excel VBA? Is there different syntax/statements I should be using to manipulate the Excel graph from Access?
I have these references selected: Excel 15.0 Object Library, Microsoft Graph 15.0 Object Library, Microsoft Office 15.0 Object Library, as well as the defaults. Is there any other references that should be added?
The frustrating part is it was working fine on another computer with the same versions of Office (2013), but after switching now it won't work :banghead:
I have it set up as late binding, also tried early binding but to no avail. I'm hoping I'm just missing something minor that I haven't flushed out yet.
Code:
Option Explicit
Public Function ExportRecordsetToExcel(rsLeft As DAO.Recordset, rsRight As DAO.Recordset, numRecords As Integer)
On Error GoTo handler:
'CREATE VARIABLES
Const xlUP As Long = -4162 'excel constants if used need to be referenced manually!
Dim oExcel As Object 'Excel application
Dim oBook As Object 'Excel workbook
Dim oSheet As Object 'Excel worksheet
Dim oChart As Object 'Excel Chart
'ERROR CHECK
If rsLeft.BOF And rsLeft.EOF Then
Exit Function 'no data to write
Else
rsLeft.MoveFirst
rsRight.MoveFirst
End If
'Define Variables
Set oExcel = New Excel.Application
oExcel.Visible = True
Set oBook = oExcel.Workbooks.Add
'Add data to cells of the first worksheet in the new workbook.
Set oSheet = oBook.worksheets(1)
'Add headers
oSheet.range("A1").Value = "Date Recorded"
oSheet.range("B1").Value = "LH Measure"
oSheet.range("C1").Value = "RH Measure"
'Copy data from recordset
oSheet.range("A2").copyfromrecordset rsLeft
oSheet.range("C2").copyfromrecordset rsRight
'Add Chart Object
Set oChart = oSheet.ChartObjects.Add(50, 40, 600, 400).Chart
oChart.SetSourceData Source:=oSheet.range("A2").Resize(numRecords, 3)
'Format Chart
oChart.ChartType = xlXYScatterLines
oChart.Legend.Position = xlBottom
' Make Excel Visible:
oExcel.Visible = True
oExcel.UserControl = True
'tidy up
Set oSheet = Nothing
Set oBook = Nothing
Set oExcel = Nothing
Exit Function
handler:
End Function
Any help would greatly appreciated
-Pat
Last edited: