如何在交叉表查询VBA代码中替换数据源为PivotMaster?
I see the issue here—your PivotMaster is just a VBA string holding your SQL logic, not an actual query object that Access can reference in your cross-tab statement. When you tried to swap it in for 1ACTMasterQuery, Access couldn't locate the "PivotMaster" object because it doesn't exist in your database's QueryDefs collection yet.
Let's walk through two solid solutions to get this working:
Method 1: Create a Temporary Query for PivotMaster First
This mimics your original setup by turning the PivotMaster SQL string into a real temporary query, then referencing that query in your cross-tab.
Sub ExportCrossTabWithTempQuery() Dim PivotMaster As String Dim SKbyYrByMo As String ' Replace this with your actual PivotMaster SQL string PivotMaster = "SELECT [YourFields] FROM [YourTables] WHERE [YourConditions];" ' Step 1: Clean up existing temp query if it exists On Error Resume Next CurrentDb.QueryDefs.Delete "TempPivotMaster" On Error GoTo 0 ' Create the temporary query from PivotMaster SQL CurrentDb.CreateQueryDef "TempPivotMaster", PivotMaster ' Step 2: Build cross-tab SQL referencing the temp query SKbyYrByMo = "TRANSFORM Sum([TempPivotMaster].Extension) AS SumOfExtension" & _ " SELECT [TempPivotMaster].Year, [TempPivotMaster].Sku, Sum([TempPivotMaster].Extension) AS [Total Of Extension]" & _ " FROM TempPivotMaster" & _ " GROUP BY [TempPivotMaster].Year, [TempPivotMaster].Sku" & _ " PIVOT [TempPivotMaster].MonthNo" ' Clean up existing cross-tab query if it exists On Error Resume Next CurrentDb.QueryDefs.Delete "SKbyYrByMo" On Error GoTo 0 ' Create the cross-tab query CurrentDb.CreateQueryDef "SKbyYrByMo", SKbyYrByMo ' Update status Me.QueryStatus.Visible = True Me.QueryStatus.SetFocus Me.QueryStatus.BackColor = vbGreen Me.QueryStatus.Text = " Processing" ' Export to Excel DoCmd.TransferSpreadsheet _ acExport, _ acSpreadsheetTypeExcel12Xml, _ "SKbyYrByMo", _ "C:\Users\John\Desktop\RSExcel.xlsx", _ True ' Clean up temporary queries CurrentDb.QueryDefs.Delete "SKbyYrByMo" CurrentDb.QueryDefs.Delete "TempPivotMaster" ' Final status updates Me.QueryStatus.Text = " " Me.SKbYbM.SetFocus Me.QueryStatus.Visible = False MsgBox "Complete" End Sub
Key Notes:
- We first delete any existing temp queries to avoid duplicate object errors
- The cross-tab now references
TempPivotMaster, a real QueryDef Access can recognize - Always clean up temp queries at the end to avoid cluttering your database
Method 2: Nest PivotMaster SQL Directly as a Subquery
If you prefer not to create extra temporary queries, you can nest the PivotMaster SQL directly in the cross-tab's FROM clause as a subquery.
Sub ExportCrossTabWithNestedSQL() Dim PivotMaster As String Dim SKbyYrByMo As String ' Replace this with your actual PivotMaster SQL string PivotMaster = "SELECT [YourFields] FROM [YourTables] WHERE [YourConditions];" ' Build cross-tab with PivotMaster nested as a subquery (aliased as PM) SKbyYrByMo = "TRANSFORM Sum([PM].Extension) AS SumOfExtension" & _ " SELECT [PM].Year, [PM].Sku, Sum([PM].Extension) AS [Total Of Extension]" & _ " FROM (" & PivotMaster & ") AS PM" & _ " GROUP BY [PM].Year, [PM].Sku" & _ " PIVOT [PM].MonthNo" ' Clean up existing cross-tab query if it exists On Error Resume Next CurrentDb.QueryDefs.Delete "SKbyYrByMo" On Error GoTo 0 ' Create the cross-tab query CurrentDb.CreateQueryDef "SKbyYrByMo", SKbyYrByMo ' Update status Me.QueryStatus.Visible = True Me.QueryStatus.SetFocus Me.QueryStatus.BackColor = vbGreen Me.QueryStatus.Text = " Processing" ' Export to Excel DoCmd.TransferSpreadsheet _ acExport, _ acSpreadsheetTypeExcel12Xml, _ "SKbyYrByMo", _ "C:\Users\John\Desktop\RSExcel.xlsx", _ True ' Clean up CurrentDb.QueryDefs.Delete "SKbyYrByMo" ' Final status updates Me.QueryStatus.Text = " " Me.SKbYbM.SetFocus Me.QueryStatus.Visible = False MsgBox "Complete" End Sub
Key Notes:
- The
PivotMasterSQL is wrapped in parentheses and aliased asPM—Access treats this as a virtual table - No extra temp queries needed, keeping your database cleaner
- Ensure your
PivotMasterSQL doesn't have a trailing semicolon (or remove it before nesting)
Why Your Original Attempt Failed
When you tried to reference PivotMaster directly in the cross-tab SQL, Access looked for a query/table named PivotMaster in your database. Since it was only a VBA string variable (not a real QueryDef), Access couldn't find it. Both solutions above give Access a valid data source it can recognize.
内容的提问来源于stack exchange,提问作者jpl458

