G
Guest
Generating and Excel2000 Chart from and MSAccess2000 Form. When finished
viewing the chart, I can not close Excel properly. So that when I change the
MSAccess Form data, I can no longer generate the Excel Chart...there is an
instance of Excel remaining open in the background preventing the reuse of
the MSAccess Form to generate a new Excel Chart. When I try to shut down the
PC, there are multiple back ground instances of Excel that have to be shut
down before the PC can be shut down. I am going back to the MSAccess Form
and using an MSAccess command to close Excel.
Private Sub cmdCreateGraph_Click()
On Error GoTo cmdCreateGraph_Err
Dim rstData As ADODB.Recordset
Dim rstCount As ADODB.Recordset
Dim fld As ADODB.Field
Dim rng As Excel.Range
Dim objWS As Excel.Worksheet
Dim intRowCount As Integer
Dim intColCount As Integer
'Display Hourglass
'DoCmd.Hourglass True
Set rstData = New ADODB.Recordset
rstData.ActiveConnection = CurrentProject.Connection
Set rstCount = New ADODB.Recordset
rstCount.ActiveConnection = CurrentProject.Connection
'Attempt to create Recordset and launch Excel
If CreateRecordset(rstData, rstCount, "qrySalesByCountry") Then
If CreateExcelObj() Then
gobjExcel.Workbooks.Add
Set objWS = gobjExcel.ActiveSheet
intRowCount = 1
intColCount = 1
'Loop though Fields collection using field names
'as column headings
For Each fld In rstData.Fields
If fld.Type <> adLongVarBinary Then
objWS.Cells(1, intColCount).Value = fld.Name
intColCount = intColCount + 1
End If
Next fld
'Send Recordset to Excel
objWS.Range("A1").CopyFromRecordset rstData, 500
'Format Data
With gobjExcel
.Columns("A:B").Select
.Columns("A:B").EntireColumn.AutoFit
.Range("A1").Select
.ActiveCell.CurrentRegion.Select
Set rng = .Selection
.Selection.NumberFormat = "$#,##0.00"
'Add a Chart Object
.ActiveSheet.ChartObjects.Add(135.75, 14.25, 607.75,
301).Select
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData
Source:=Sheets("Sheet1").Range("A1:B21"), PlotBy _
:=xlColumns
ActiveChart.Location Where:=xlLocationAsNewSheet
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Corrosion Rate"
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = _
"Corrosion Rate (mpy)"
End With
ActiveChart.HasLegend = False
ActiveChart.HasDataTable = False '''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Make Excel Visible
.Visible = True
End With
Else
MsgBox "Excel Not Successfully Launched"
End If
Else
MsgBox "Too Many Records to Send to Excel"
End If
DoCmd.Hourglass False
cmdCreateGraph_Exit:
Set rstData = Nothing
Set rstCount = Nothing
Set fld = Nothing
Set rng = Nothing
Set objWS = Nothing
DoCmd.Hourglass False
Exit Sub
cmdCreateGraph_Err:
MsgBox "Error # " & Err.Number & ": " & Err.Description
Resume cmdCreateGraph_Exit
End Sub
'Close Excel from the MSAccess Form using this cmd_click
Private Sub Command3_Click()
CloseExcel
End Sub
viewing the chart, I can not close Excel properly. So that when I change the
MSAccess Form data, I can no longer generate the Excel Chart...there is an
instance of Excel remaining open in the background preventing the reuse of
the MSAccess Form to generate a new Excel Chart. When I try to shut down the
PC, there are multiple back ground instances of Excel that have to be shut
down before the PC can be shut down. I am going back to the MSAccess Form
and using an MSAccess command to close Excel.
Private Sub cmdCreateGraph_Click()
On Error GoTo cmdCreateGraph_Err
Dim rstData As ADODB.Recordset
Dim rstCount As ADODB.Recordset
Dim fld As ADODB.Field
Dim rng As Excel.Range
Dim objWS As Excel.Worksheet
Dim intRowCount As Integer
Dim intColCount As Integer
'Display Hourglass
'DoCmd.Hourglass True
Set rstData = New ADODB.Recordset
rstData.ActiveConnection = CurrentProject.Connection
Set rstCount = New ADODB.Recordset
rstCount.ActiveConnection = CurrentProject.Connection
'Attempt to create Recordset and launch Excel
If CreateRecordset(rstData, rstCount, "qrySalesByCountry") Then
If CreateExcelObj() Then
gobjExcel.Workbooks.Add
Set objWS = gobjExcel.ActiveSheet
intRowCount = 1
intColCount = 1
'Loop though Fields collection using field names
'as column headings
For Each fld In rstData.Fields
If fld.Type <> adLongVarBinary Then
objWS.Cells(1, intColCount).Value = fld.Name
intColCount = intColCount + 1
End If
Next fld
'Send Recordset to Excel
objWS.Range("A1").CopyFromRecordset rstData, 500
'Format Data
With gobjExcel
.Columns("A:B").Select
.Columns("A:B").EntireColumn.AutoFit
.Range("A1").Select
.ActiveCell.CurrentRegion.Select
Set rng = .Selection
.Selection.NumberFormat = "$#,##0.00"
'Add a Chart Object
.ActiveSheet.ChartObjects.Add(135.75, 14.25, 607.75,
301).Select
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData
Source:=Sheets("Sheet1").Range("A1:B21"), PlotBy _
:=xlColumns
ActiveChart.Location Where:=xlLocationAsNewSheet
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Corrosion Rate"
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = _
"Corrosion Rate (mpy)"
End With
ActiveChart.HasLegend = False
ActiveChart.HasDataTable = False '''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Make Excel Visible
.Visible = True
End With
Else
MsgBox "Excel Not Successfully Launched"
End If
Else
MsgBox "Too Many Records to Send to Excel"
End If
DoCmd.Hourglass False
cmdCreateGraph_Exit:
Set rstData = Nothing
Set rstCount = Nothing
Set fld = Nothing
Set rng = Nothing
Set objWS = Nothing
DoCmd.Hourglass False
Exit Sub
cmdCreateGraph_Err:
MsgBox "Error # " & Err.Number & ": " & Err.Description
Resume cmdCreateGraph_Exit
End Sub
'Close Excel from the MSAccess Form using this cmd_click
Private Sub Command3_Click()
CloseExcel
End Sub