我已经学习了一个月的编程。当我最后一次离开时,代码运行完美没有问题,现在程序在尝试创建图表时崩溃(我通过记录集从访问数据库导入数据,然后在隐藏表中操作它,最后导出并导入到我的用户表单中的imagebox。
错误消息:运行时错误“1004”:应用程序定义或对象定义错误
代码,它在createchart过程中崩溃。
Public Sub Create(TickerID As String, StartDate As Date, EndDate As Date)
Call ImportData(TickerID, StartDate, EndDate)
Call CreateHeadings(TickerID)
Call CreateChart
Call DisplayChart
End Sub
Private Sub ImportData(TickerID As String, StartDate As Date, EndDate As Date)
Const ConnectionPath As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\name\Desktop\DataBase.accdb;Persist Security Info=False;"
Dim DataConnection As ADODB.Connection: Set DataConnection = New ADODB.Connection
Dim RecordSet As ADODB.RecordSet: Set RecordSet = New ADODB.RecordSet
DataConnection.ConnectionString = ConnectionPath
DataConnection.Open
Dim SQLString As String: SQLString = "SELECT * FROM " _
& TickerID _
& " WHERE [Date] BETWEEN #" & StartDate & "# AND #" & EndDate & "#"
With RecordSet
.ActiveConnection = DataConnection
.Source = SQLString
.LockType = adLockReadOnly
.CursorType = adOpenForwardOnly
.Open
End With
' Copies Data from RecordSet to Hidden Sheet "Chart", good for further investigating the data if necessary too
Worksheets("Chart").Cells.ClearContents
Worksheets("Chart").Range("A2").CopyFromRecordset RecordSet
Set RecordSet = Nothing
Set DataConnection = Nothing
End Sub
Private Sub CreateHeadings(TickerID As String)
Const ConnectionPath As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\name\Desktop\DataBase.accdb;Persist Security Info=False;"
Dim DataConnection As ADODB.Connection: Set DataConnection = New ADODB.Connection
Dim RecordSet As ADODB.RecordSet: Set RecordSet = New ADODB.RecordSet
DataConnection.ConnectionString = ConnectionPath
DataConnection.Open
Dim SQLString As String: SQLString = "SELECT * FROM " _
& TickerID & " where 1 = 2"
With RecordSet
.ActiveConnection = DataConnection
.Source = SQLString
.LockType = adLockReadOnly
.CursorType = adOpenForwardOnly
.Open
End With
For i = 0 To RecordSet.Fields.Count - 1
Dim x As String
Worksheets("chart").Cells(1, 1 + i).Value = RecordSet.Fields(i).Name
Next i
End Sub
Private Sub CreateChart()
' Create new graph and export to be imported in userform image control
Dim OHLCChart As ChartObject
Set OHLCChart = Sheets("Chart").ChartObjects.Add(Left:=Range("A10") _
.Left, Width:=500, Top:=Range("a10").Top, Height:=300)
With OHLCChart.Chart ****<- CRASHES HERE!****
.SetSourceData Source:=Sheets("Chart").Range("A2:E2", Range("E2").End(xlDown))
.ChartType = xlStockOHLC
.ChartArea.Format.Line.Visible = msoFalse
.Axes(xlValue).MaximumScale = Application.WorksheetFunction.Max(Columns("C")) + 5
.Axes(xlValue).MinimumScale = Application.WorksheetFunction.Min(Columns("D")) - 5
.Export ThisWorkbook.Path & "\temp.bmp" ' Export as Gif
End With
' OHLCChart.Delete
End Sub
Private Sub DisplayChart()
' Loads the graph image Path: temp.gif on desktop - should be changed to combobox - change procedure
If MainWindow.Img_Chart_Picture.Picture = Enabled Then
MainWindow.Img_Chart_Picture.Default
Else
MainWindow.Img_Chart_Picture.Picture = LoadPicture(ThisWorkbook.Path & "\temp.bmp")
End If
End Sub
可能有什么不对?再次,大约一个月前的最后一次,一切都完美无瑕。
答案 0 :(得分:2)
试试这个 - 请注意,您应始终使用工作表对象
来限定Range()
的每次使用
Private Sub CreateChart()
' Create new graph and export to be imported in userform image control
Dim OHLCChart As ChartObject
Dim rng As Range, sht as WorkSheet
Set sht = Sheets("Chart")
Set OHLCChart = sht.ChartObjects.Add(Left:=sht.Range("A10").Left, _
Width:=500, Top:=sht.Range("a10").Top, Height:=300)
Set rng = sht.Range(sht.Range("A2:E2"), sht.Range("E2").End(xlDown))
With OHLCChart.Chart
.SetSourceData Source:=rng
.ChartType = xlStockOHLC
.ChartArea.Format.Line.Visible = msoFalse
.Axes(xlValue).MaximumScale = Application.WorksheetFunction.Max(sht.Columns("C")) + 5
.Axes(xlValue).MinimumScale = Application.WorksheetFunction.Min(sht.Columns("D")) - 5
.Export ThisWorkbook.Path & "\temp.bmp" ' Export as Gif
End With
' OHLCChart.Delete
End Sub