简体   繁体   English

TCD数据透视表ADODB访问

[英]TCD pivotTable ADODB Access

I a module I declared constant variables 我在模块中声明了常量变量

Public Const ADODB_PROVIDER = "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;"
Public Const PATH_DB = "E:\BkUpData\Projets\Access\GarageHellMotors\facturation_be_test.accdb"

In an other module this function called on workbook load event 在另一个模块中,此功能在工作簿加载事件上调用

Public Function fWkBookCnxAdd()

Dim objWBConnect As WorkbookConnection

Set objWBConnect = ThisWorkbook.Connections.Add( _
Name:="tcd", Description:="", _
ConnectionString:=ADODB_PROVIDER & _
"Data Source=" & PATH_DB, _
CommandText:="SELECT * FROM qryFactureSumMonthYear", _
lCmdtype:=xlCmdSql)

End Function

See l enter link description here 请参阅l 在此处输入链接说明

On a tab, I added a commandButton with this code below on click even 在选项卡上,我在单击时甚至在下面添加了带有以下代码的commandButton

Private Sub cmdTcd_Click()

Dim oPivotCache As PivotCache
Dim oPtTable As PivotTable

ActiveSheet.Range("A3").CurrentRegion.Clear 

' Create a PivotTable cache
Set oPivotCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal,  _
SourceData:=ThisWorkbook.Connections(1))

Set oPtTable = oPivotCache.CreatePivotTable( _ 
TableDestination:=Range("A3"), _
TableName:="tcd") 

But This code below is wrong and I don't know where 但是下面的代码是错误的,我不知道在哪里

Set oPivotCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal, _ SourceData:=ThisWorkbook.Connections(1)) 设置oPivotCache = ActiveWorkbook.PivotCaches.Add(SourceType:= xlExternal,_ SourceData:= ThisWorkbook.Connections(1))

so the command below generates a Run-Time error '1004' Application-defined or object-defined error 因此下面的命令会生成运行时错误“ 1004”,由应用程序定义或对象定义的错误

Set oPtTable = oPivotCache.CreatePivotTable( _ TableDestination:=Range("A3"), _ TableName:="tcd") ' ---> Error 1004 ...Code 设置oPtTable = oPivotCache.CreatePivotTable(_ TableDestination:= Range(“ A3”),_ TableName:=“ tcd”)'--->错误1004 ...

Thanks by advance for your help 在此先感谢您的帮助

I found this solution below 我在下面找到了这个解决方案

In a module 在模块中

Public Const ADODB_PROVIDER = "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;"
Public Const PATH_DB = "E:\Access\test.accdb"

Public Function fWkBookCnxDelAll()
Dim oWkBookCnx As WorkbookConnection

For Each oWkBookCnx In ThisWorkbook.Connections
    oWkBookCnx.Delete
Next oWkBookCnx
End Function

Public Function fWkBookCnxInitAll()    
Dim objWBConnect As WorkbookConnection

Set objWBConnect = ThisWorkbook.Connections.Add( _
Name:="tcd", Description:="", _
ConnectionString:=ADODB_PROVIDER & _
"Data Source=" & PATH_DB, _
CommandText:="SELECT * FROM qryFactureSumMonthYear", _
lCmdtype:=xlCmdSql)
End Function

On Thisworkbook load event, the code below 在Thisworkbook加载事件中,以下代码

Private Sub Workbook_Open()
 fWkBookCnxDelAll
 fWkBookCnxInitAll    
End Sub

On command button click event of a worksheet, the code below 在工作表的命令按钮单击事件上,下面的代码

Private Sub cmdAddTCD_Click()

Dim oCnx As WorkbookConnection
Dim oPc As PivotCache
Dim oPt As PivotTable

ActiveSheet.Range("G7").CurrentRegion.Clear '

' Create a PivotTable cache
Set oCnx = ThisWorkbook.Connections("tcd")
Set oPc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, _
SourceData:=oCnx)

' Create a tcd.
Set oPt = oPc.CreatePivotTable( _
TableDestination:=ActiveSheet.Range("G4"), _
TableName:="tcd")

With oPt
    .SmallGrid = False

    .AddFields _
    RowFields:="idfacture", _
    RowFields:="libelle", _
    ColumnFields:="PeriodemontYear"

    .AddDataField _
    Field:=oPt.PivotFields("montant") _
 End With
End Sub

A new TCD is now created 现在创建一个新的TCD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM