LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 336
Default Pivot Table Code

I am new to using VB, so I usually record a script that will perform the task
I wish, then I modify it to be more "general" by substituting variables for
the portions that would change. This has worked well for me in the past, but
my latest task is causing problems.

I recorded the following that creates and Excel Pivot Table from a query
stored in an Access database. As it stands, it works just fine. However, I
would like to be able to change the Access file name and to create the pivot
table in the current workbook and current worksheet.

RECORDING START
With ActiveWorkbook.PivotCaches.Add(SourceType:=xlExter nal)
.Connection = Array(Array( _
"ODBC;DSN=MS Access
Database;DBQ=D:\work\Projects\IHHA_Temp.mdb;Defaul tDir=D:\work\Projects;DriverId=25;FIL=MS Access;MaxBufferSize=2" _
), Array("048;PageTimeout=5;"))
.CommandType = xlCmdSql
.CommandText = Array( _
"SELECT `Quarterly Volume Query`.COUNTY_NAME, `Quarterly Volume
Query`.HOSPITAL_NAME, `Quarterly Volume Query`.`DISCHARGE_DATE By Quarter`,
`Quarterly Volume Query`.`Count Of Inpatient`" & Chr(13) & "" & Chr(10) &
"FROM `D:\work\" _
, "Projects\IHHA_Temp`.`Quarterly Volume Query` `Quarterly Volume
Query`")
.CreatePivotTable TableDestination:="[prodline.xls]Sheet3!R6C1", _
TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10
End With
ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:=Array( _
"COUNTY_NAME", "HOSPITAL_NAME"), ColumnFields:="DISCHARGE_DATE By
Quarter"
ActiveSheet.PivotTables("PivotTable1").PivotFields ("Count Of
Inpatient"). _
Orientation = xlDataField
RECORDING END

Starting with the Connection statement, I created a variable that contains
the text of everything after the "=" sign as follows:

ConnectText = "Array(Array(" & Chr(34) & _
"ODBC;DSN=MS Access Database;DBQ=" &
"D:\work\Projects\IHHA_Temp.mdb" & _
";DefaultDir=" & "D:\work\Projects" & _
";DriverId=25;FIL=MS Access;MaxBufferSize=2" & _
Chr(34) & Chr(41) & ", " & _
"Array(" & Chr(34) & "048;PageTimeout=5;" & _
Chr(34) & Chr(41) & Chr(41)

I then modified the Connection statement as follows:

.Connection = ConnectText


Every time I execute, this modified statement generates a "Runtime error
1004" error. I have tried using Dim statements to set this variable to
String or Variant to no affect.

Can someone explain what could be wrong?

Also, is the DefaultDir portion of the Connection statement related to the
Excel or Access file? I assume the Access file.


Thanks for the help.
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel Pivot Table VB Code carolini Excel Discussion (Misc queries) 1 February 6th 06 09:33 PM
Pivot Table Code FA Excel Discussion (Misc queries) 6 October 11th 05 04:03 PM
VBA Code for a pivot table to open database and to reference table current page Pete Straman Straman via OfficeKB.com Excel Programming 0 February 21st 05 03:57 AM
vba code for Pivot Table Paul Excel Programming 0 May 31st 04 05:04 AM
HELP! Code for Pivot Table Sandy[_3_] Excel Programming 6 September 3rd 03 09:43 PM


All times are GMT +1. The time now is 10:20 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"