TCD pivotTable ADODB Access
Bonjour à tous,
Dans un 1er module j'ai
Code:
1 2
| Public Const ADODB_PROVIDER = "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;"
Public Const PATH_DB = "E:\BkUpData\Projets\Access\test.accdb" |
Dans un autre module j'ai cette fonction qui crée une connexion. Petite particularité le commandText et CmdType sont déjà renseignés et elle est appelée sur l'événement Load du workbook
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| Public Function fWkBookCnxAdd()
Dim objWBConnect As WorkbookConnection
Set objWBConnect = ThisWorkbook.Connections.Add( _
Name:="tcd", Description:="", _
ConnectionString:=cstADODB_PROVIDER & _
"Data Source=" & cstPATH_DB, _
CommandText:="SELECT * FROM qryFactureSumMonthYear", _
lCmdtype:=xlCmdSql)
End Function |
Dans un onglet j'ai ajouté un bouton et sur l'événement click j'ai une erreur sur
Set oPtTable = oPivotCache.CreatePivotTable
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| Private Sub cmdTcd_Click()
Dim oPivotCache As PivotCache
Dim oPtTable As PivotTable
ActiveSheet.Range("A3").CurrentRegion.Clear ' --> suppression du TCD
' Create a PivotTable cache
Set oPivotCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal, _
SourceData:=ThisWorkbook.Connections("tcd")) ' ---> Pas d'erreur
' Create a tcd.
Set oPtTable = oPivotCache.CreatePivotTable( _
TableDestination:=Range("A3"), _
TableName:="tcd") ' ---> Erreur d'exécution 1004 - Erreur définie par l'application ou par l'objet
With ActiveSheet.PivotTables("tcd")
.SmallGrid = False
.AddFields _
RowFields:="idfacture", _
RowFields:="libelle", _
ColumnFields:="PeriodemontYear"
.adddatafields _
Field:=oPtTable.PivotFields("montant")
End With
End Sub |
Merci pour votre aide
Complément d’information après clôture du sujet:
Pour la version 2010 il faut utiliser PivotCaches.Create et non add
Voir le dernier post de cette discution pour un exemple de code corrigé et qui fonctionne