[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Cross-Tab Report

Posted on 2009-12-21
2
Medium Priority
?
478 Views
Last Modified: 2013-11-28
Hello-
I am having some trouble with executing a report. The code actually executes just fine, but when I go to open the report I get an error telling me that "Cannot use the crosstab of a non-fixed column as a subquery." Now, when I simply create a crosstab query and base the report off it the report opens just fine. The data feeding the crosstab query is simply a table so no sources of error there . And to answer, the obvious of why not just build it manually then is because I am using VBA to create dynamic reports that will all be based off of similar data and I will have +25 different reports but with the same structure so doing it through VBA will provide more flexibility long-term if any changes need to be made to all or one of the reports. Any idea of what may be triggering the error?
Thanks!
EK
Sub SectorMarketValueReport()
Dim db As DAO.Database ' database object
Dim rs As DAO.Recordset ' recordset object
Dim fld As DAO.Field ' recordset field
Dim grpLevelOne As Variant
Dim grpLevelTwo As Variant
Dim txtNew As Access.TextBox ' textbox control
Dim lblNew As Access.Label ' label control
Dim rpt As Report ' hold report object
Dim lngTop, lngLeft, lngWidth, lngHeight As Long
Dim title, fieldname, Portfolio, strReportName As String
  
  
'Recordsource is crossTab query
strSQL = "TRANSFORM Sum(tblReport_SectorDiff_MV.MV_DIFF) AS SumOfMV_DIFF SELECT tblReport_SectorDiff_MV.Report_Order, tblReport_SectorDiff_MV.Sector, "
strSQL = strSQL & "tblReport_SectorDiff_MV.SubSector FROM tblReport_SectorDiff_MV GROUP BY "
strSQL = strSQL & "tblReport_SectorDiff_MV.Report_Order, tblReport_SectorDiff_MV.Sector, tblReport_SectorDiff_MV.SubSector "
strSQL = strSQL & "PIVOT tblReport_SectorDiff_MV.PortName;"

     'set the title
     title = "Portfolio vs Barclay's Index Sector MarketValue Difference"
  
     ' initialise position variables
     lngLeft = 521
     lngTop = 28
     lngWidth = 583
     lngHeight = 208
  
     'Create the report
     strReportName = "SectorsMV"
     Set rpt = CreateReport

  

     ' set properties of the Report
     With rpt
         .Width = 9500
         .RecordSource = strSQL
         .Caption = title
         .OrderBy = False
         .OrderByOn = False
         .FilterOn = False
     End With
     
     'Set page set-up
     With rpt.Printer
        .Orientation = acPRORLandscape
        .LeftMargin = 0.25
        .RightMargin = 0.25
     End With

  
     ' Open SQL query as a recordset
     Set db = CurrentDb
     Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
     
     'Create Report Groupings and Sorting
     grpLevelOne = CreateGroupLevel(rpt.Name, "Report_Order", False, False)
     grpLevelTwo = CreateGroupLevel(rpt.Name, "Sector", True, False)
     
     'Set the sort order by the report order textbox value
     rpt.GroupLevel(0).SortOrder = False

     ' Create Textbox for Group Header Title
     fieldname = "Sector"
         Set txtNew = CreateReportControl(rpt.Name, acTextBox, acGroupLevel2Header, , fieldname, 0, lngTop, lngWidth + 1000, lngHeight + 50)
         
         'Format Control
         With txtNew
            .FontSize = 10
            .FontWeight = 600
         End With
  
       ' Create Textbox for Row title in Detail section
       fieldname = "SubSector"
       
         Set txtNew = CreateReportControl(rpt.Name, acTextBox, acDetail, , fieldname, lngLeft, lngTop, lngWidth + 1500, lngHeight)
         
         'Format Control
         With txtNew
            .FontSize = 8
            .FontWeight = 400
         End With
  
  
     ' Create corresponding text box controls for each portfolio.
    
    lngLeft = lngLeft + 1500
    
    NoOfFields = 21
    For x = 4 To NoOfFields - 1
    
      Portfolio = rs.Fields(x).Name
  
         ' Create new text box control and size to fit data.
         Set txtNew = CreateReportControl(rpt.Name, acTextBox, acDetail, , Portfolio, lngLeft, lngTop, lngWidth, lngHeight)

         With txtNew
            .FontSize = 8
            .FontWeight = 400
            .Format = "Percent"
            .DecimalPlaces = 2
         End With

         ' Increment top value for next control
         lngLeft = lngLeft + 650
    
    Next x

    'Set the Detail Height
    rpt.Section(acDetail).Height = 164
  
     ' Create datestamp in Footer
     Set lblNew = CreateReportControl(rpt.Name, acLabel, acPageFooter, , Now(), 0, 0)
  
  
     ' Open new report.
     DoCmd.Close acReport, title, acSavePrompt
     
  
     'reset all objects
     rs.Close
     Set rs = Nothing
     Set rpt = Nothing
     Set db = Nothing
  
End Sub

Open in new window

0
Comment
Question by:ExcelKid1081
2 Comments
 
LVL 31

Accepted Solution

by:
hnasr earned 1000 total points
ID: 26102443
You understand the problem, then simplify it in a sample database that reflects the issu and attach.
Simplify = few fields involved, and only necessary objects.
0
 
LVL 7

Expert Comment

by:RemRemRem
ID: 36293081
The "Cannot use the crosstab of a non-fixed column as a subquery" is the key - Access gets hinky when it thinks there *might* be some additional (or fewer) columns the next time it runs. When you're using it directly into a report, the report can see "oh, I've got 5 columns, so I only need to worry about those 5 all the time..."

Try making a temp table out of your crosstab each time you run the report series, and use a select statement against that resulting temp table for the report. You'll want to make sure you're doing regular compact/repairs on the DB if you do, because each creation of the maketable expands the DB size but doesn't auto-contract itself once deleted.

-Rachel

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question