gpt4 book ai didi

excel - 从多个连接构建可刷新查询 AS400 IBMi

转载 作者:行者123 更新时间:2023-12-02 22:52:43 26 4
gpt4 key购买 nike

我的尝试是从 AS400 IBMi 的多个表中动态创建一个简单的数据透视表。这已实现,但数据透视表不可“刷新”。

因此,我开始查看有关以编程方式创建连接的帖子,并提出了下面的示例,该示例是可刷新的,但只有一个表:

    ActiveWorkbook.Connections.AddFromFile "N:\apps\excel\connections\PRD IS.odc"
With ActiveWorkbook.Connections("PRD IS").ODBCConnection
.BackgroundQuery = True
.CommandText = Array("SELECT * FROM ""PRD"".""Y2K"".""IS""")
.CommandType = xlCmdSql
.Connection = "ODBC;DSN=s11111111;"
.RefreshOnFileOpen = False
.SavePassword = False
.SourceConnectionFile = "N:\apps\excel\connections\PRD IS.odc"
.SourceDataFile = ""
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = False
End With
With ActiveWorkbook.Connections("PRD IS")
.Name = "PRD IS"
End With

ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, SourceData:= _
ActiveWorkbook.Connections("PRD IS"), Version:=xlPivotTableVersion14). _
CreatePivotTable TableDestination:=ActiveCell, TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion14

是否可以使用两个连接,将它们组合起来,并创建可刷新的最终结果?

第二个连接是:

      ActiveWorkbook.Connections.AddFromFile "N:\apps\excel\connections\PRD PM.odc"
With ActiveWorkbook.Connections("PRD PM").ODBCConnection
.BackgroundQuery = True
.CommandText = Array("SELECT * FROM ""PRD"".""Y2K"".""PM""")
.CommandType = xlCmdSql
.Connection = "ODBC;DSN=s111111111;"
.RefreshOnFileOpen = False
.SavePassword = False
.SourceConnectionFile = "N:\apps\excel\connections\PRD PM.odc"
.SourceDataFile = ""
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = False
End With
With ActiveWorkbook.Connections("PRD PM")
.Name = "PRD PM"
End With

当前工作代码:

Sub CreatePivotTable()
'Declare variables
Dim Conn As ADODB.Connection
Dim Cmd As ADODB.Command
Dim Param As ADODB.Parameter
Dim rs As ADODB.Recordset

Set Conn = New ADODB.Connection
Set Cmd = New ADODB.Command
Set rs = New ADODB.Recordset

'Open Connection'
Conn.ConnectionString = "DSN=s11111111;"
Conn.Open

'Set and Excecute SQL Command'
Set Cmd.ActiveConnection = Conn

Cmd.CommandText = "SELECT ISWH as WH,ISPART as Part,PMDESC as Description,ISCF01 As AC, PMPCLS As PC, PMPLIN As PL" & _
" FROM Y2K.IS LEFT JOIN Y2K.PM ON Y2K.IS.ISPART = Y2K.PM.PMPART" & _
" WHERE(ISWH) in ('XX')" & _
" AND (ISCF01) not in ('B','D')" & _
" AND (PMPLIN) in ('YY')" & _
" AND (PMPCLS) like ('Z%')"

Cmd.CommandType = adCmdText

'Open Recordset'
Set rs.Source = Cmd
rs.Open

'Create a PivotTable cache and report.
Set objPivotCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal)
Set objPivotCache.Recordset = rs
objPivotCache.CreatePivotTable TableDestination:=ActiveCell, TableName:="PivotTable1"

With ActiveSheet.PivotTables("PivotTable1")
.SmallGrid = False
With .PivotFields("WH")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Part")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("PL")
.Orientation = xlColumnField
.Position = 1
End With
With .PivotFields("PC")
.Orientation = xlDataField
.Position = 1
End With
End With
End Sub

最佳答案

好吧,我明白了。我没有添加已存在的连接,而是添加了一个新连接并对其进行了定义。

"Test" is the name of the connection
"x" is the description
"Conn" is the connection string
"Cmdarray" is the sql

Sub CreatePivotTable()
Dim Cmdarray
Dim Conn

Cmdarray = Array("SELECT ISWH as WH, ISPART as Part,PMDESC as Description, ISCF01 as AC FROM ""PRD"".""Y2K"".""IS"" LEFT JOIN ""PRD"".""Y2K"".""PM"" ON ""PRD"".""Y2K"".""IS"".ISPART = ""PRD"".""Y2K"".""PM"".PMPART WHERE (ISWH) IN ('XX')")

Conn = "ODBC;DSN=s111111111;"

ActiveWorkbook.Connections.Add "Test", "x", Conn, Cmdarray

ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, SourceData:= _
ActiveWorkbook.Connections("Test"), Version:=xlPivotTableVersion14). _
CreatePivotTable TableDestination:=ActiveCell, TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion14
End Sub

关于excel - 从多个连接构建可刷新查询 AS400 IBMi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29780415/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com