Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Problem creating PivotTable from ADO recordset using VBA

I am trying to create a PivotTable from VBA code in which I first query an
external database to create an ADO recordset, then set the
PivotCache.Recordset to the ADO recordset. The problem I am having is that
all of the fields in the ADO recordset do not show up in the PivotFields
collection (however, all fields show up in the PivotCache.Recordset). No
errors show until I try to add a field to the PivotTable using a recordset
field that did not carry over from ADO. Below is the code section I am using
(still in debug mode so not as clean as it shold be). I have commented two
areas to show where I check if the recordset fields match and if the
pivottable.fields match (commented as ***).

Any suggestions will be appreciated. Thanks in advance!! :)

Larry

Excel 2003 with reference to Microsoft ActiveX Data Objects 2.6 Library

================================================== ==

Sub MakePivotTableExample()

Dim cnDBTP As ADODB.Connection
Dim rsClaims As ADODB.Recordset
Dim rsTemp As ADODB.Recordset
Dim pvtTable As PivotTable
Dim pvtField As PivotField
Dim pvtCache As PivotCache
Dim sSQL As String

Set cnDBTP = New ADODB.Connection
Set rsClaims = New ADODB.Recordset

'make connection to dbtp
cnDBTP.CursorLocation = adUseClient

cnDBTP.ConnectionString = _
"Provider=IBMDADB2.1;" & _
"Persist Security Info=False;" & _
"User ID=u831;" & _
"Password=04jess;" & _
"Data Source=DBTP;" 'Location='';Extended Properties=''"

cnDBTP.Open

'get claims data
rsClaims.CursorLocation = adUseClient

sSQL = "SELECT CLMS_CLAIM_NO, " & _
" CLMS_DATE_RECEIVED, " & _
" CLMS_ORIG, CLMS_DEST, " & _
" CLMS_ORIGINAL_AMT, CLMS_PAYMENT_AMT, " & _
" CLMS_CLAIM_REAS " & _
"FROM YELLOW.CLCLMS " & _
"WHERE (CLMS_DATE_RECEIVED BETWEEN '3/1/2008' AND '3/31/2008') "
& _
" AND (CLMS_CLAIM_REAS In ('10','11','40','41')) " & _
"WITH UR"

rsClaims.Open sSQL, cnDBTP, adOpenStatic, adLockOptimistic, adCmdText

'make pivot table
Set pvtCache = ActiveWorkbook.PivotCaches.Add(xlExternal)
Set pvtCache.Recordset = rsClaims
Set rsTemp = pvtCache.Recordset

'check that the pvottable recordset has all fields as ADO recordset
'*** they match!!
For i = 0 To rsClaims.Fields.Count - 1
Debug.Print rsClaims.Fields(i).Name, pvtCache.Recordset.Fields(i).Name
Next

With pvtCache
.CreatePivotTable Range("B6"), "ClaimsByType"
End With

Set pvtTable = ActiveSheet.PivotTables("ClaimsByType")

'loop through pivotfields to se if they match ADO recordset
'*** they do not match!!
'2 fields left off: CLMS_ORIGINAL_AMT and CLMS_PAYMENT_AMT
For Each pvtField In pvtTable.PivotFields
Debug.Print pvtField.Name
Next

With pvtTable
.SmallGrid = False

With .PivotFields("CLMS_CLAIM_REAS")
.Orientation = xlRowField
.Position = 1
End With

With .PivotFields("CLMS_ORIGINAL_AMT")
.Orientation = xlDataField
.Function = xlSum
.Position = 1
End With

With .PivotFields("CLMS_PAYMENT_AMT")
.Orientation = xlDataField
.Function = xlSum
.Position = 2
End With

With .PivotFields("CLMS_CLAIM_NO")
.Orientation = xlDataField
.Function = xlCount
.Position = 3
End With

End With

End Sub



Reply
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
Problem with creating pivottable Ruben Excel Discussion (Misc queries) 0 August 27th 08 11:45 AM
PivotTable from a ADODB.Recordset ? mark Excel Programming 7 December 6th 07 06:08 PM
Creating a PivotTable w/o selecting data in an existing PivotTable Damian Excel Discussion (Misc queries) 6 November 2nd 07 04:44 PM
Creating Pivot Table from a DAO recordset [email protected] Excel Programming 0 May 17th 07 03:07 PM
Creating a recordset over a range James Excel Programming 3 December 10th 04 05:16 AM


All times are GMT +1. The time now is 09:48 PM.

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"