M
Mansoor Azam
My VB 6 application creates and displays an excel file with a pivot table in
it that gets data from an access database when the following routine is run.
The report is displayed correctly in an Excel workbook (Book1) the first
time this routine is run. However if I click the button again ( after
closing excel ) excel is opened with a blank workbook named Book2. No pivot
table ! Similarly closing and opening again gives another blank Book3 and so
on. To get the pivot table I have to quit my application and run it again
every time I want a report displayed. Whats the problem?
any help appreciated.
thx
Private Sub cmdShowXLReport_Click()
Dim Excelapp As Excel.Application
Dim ExcelWS As Excel.Worksheet ' This is the sheet
Set Excelapp = CreateObject("Excel.Application")
Excelapp.Workbooks.Add
Set ExcelWS = Excelapp.ActiveSheet
With ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal)
.Connection = Array(Array( _
"ODBC;DSN=MS Access
Database;DBQ=c:\Barki\SAMSOL\samsol.mdb;DefaultDir=c:\Barki\SAMSOL;DriverId=
25;FIL=MS Access;MaxBufferSize=2048;P" _
), Array("ageTimeout=5;"))
.CommandType = xlCmdSql
.CommandText = Array( _
"SELECT rTable." & fld & ", rTable.item, rTable.colour, rTable.qty"
& Chr(13) & "" & Chr(10) & "FROM `c:\Barki\SAMSOL\samsol`.rTable rTable" _
)
.CreatePivotTable TableDestination:=Range("A3"), TableName:= _
"PivotTable2"
End With
ActiveSheet.PivotTables("PivotTable2").SmallGrid = False
With ActiveSheet.PivotTables("PivotTable2")
.PivotFields(fld).Orientation = xlRowField
.PivotFields(fld).Position = 1
.PivotFields("colour").Orientation = xlColumnField
.PivotFields("colour").Position = 1
.PivotFields("item").Orientation = xlColumnField
.PivotFields("item").Position = 1
.PivotFields("qty").Orientation = xlDataField
.PivotFields("qty").Position = 1
End With
Application.CommandBars("PivotTable").Visible = False
ActiveSheet.PivotTables("PivotTable2").PivotSelect "", xlDataAndLabel
Selection.NumberFormat = "0.00"
With ActiveSheet.PivotTables("PivotTable2")
.MergeLabels = True
.NullString = "0"
End With
Range("A1").Select
Excelapp.Visible = True
Set Excelapp = Nothing
End Sub
it that gets data from an access database when the following routine is run.
The report is displayed correctly in an Excel workbook (Book1) the first
time this routine is run. However if I click the button again ( after
closing excel ) excel is opened with a blank workbook named Book2. No pivot
table ! Similarly closing and opening again gives another blank Book3 and so
on. To get the pivot table I have to quit my application and run it again
every time I want a report displayed. Whats the problem?
any help appreciated.
thx
Private Sub cmdShowXLReport_Click()
Dim Excelapp As Excel.Application
Dim ExcelWS As Excel.Worksheet ' This is the sheet
Set Excelapp = CreateObject("Excel.Application")
Excelapp.Workbooks.Add
Set ExcelWS = Excelapp.ActiveSheet
With ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal)
.Connection = Array(Array( _
"ODBC;DSN=MS Access
Database;DBQ=c:\Barki\SAMSOL\samsol.mdb;DefaultDir=c:\Barki\SAMSOL;DriverId=
25;FIL=MS Access;MaxBufferSize=2048;P" _
), Array("ageTimeout=5;"))
.CommandType = xlCmdSql
.CommandText = Array( _
"SELECT rTable." & fld & ", rTable.item, rTable.colour, rTable.qty"
& Chr(13) & "" & Chr(10) & "FROM `c:\Barki\SAMSOL\samsol`.rTable rTable" _
)
.CreatePivotTable TableDestination:=Range("A3"), TableName:= _
"PivotTable2"
End With
ActiveSheet.PivotTables("PivotTable2").SmallGrid = False
With ActiveSheet.PivotTables("PivotTable2")
.PivotFields(fld).Orientation = xlRowField
.PivotFields(fld).Position = 1
.PivotFields("colour").Orientation = xlColumnField
.PivotFields("colour").Position = 1
.PivotFields("item").Orientation = xlColumnField
.PivotFields("item").Position = 1
.PivotFields("qty").Orientation = xlDataField
.PivotFields("qty").Position = 1
End With
Application.CommandBars("PivotTable").Visible = False
ActiveSheet.PivotTables("PivotTable2").PivotSelect "", xlDataAndLabel
Selection.NumberFormat = "0.00"
With ActiveSheet.PivotTables("PivotTable2")
.MergeLabels = True
.NullString = "0"
End With
Range("A1").Select
Excelapp.Visible = True
Set Excelapp = Nothing
End Sub