Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Help   Contact   Merch   Join   Order   Logon   Forums   
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Crosstab as a report source
Dominic Hubble 
    
3 years ago
Hi, I need a report based on my crosstab query, but as the fields from the crosstab are dynamic, I can't map them in the report design.

My crosstab query displays the order quantities (counted values) for products (columns) ordered today by customers (rows). Some days there may be 3 products (columns), other days there may be columns for all 10 products.

How can I build a report that deals with changing numbers of fields to display?
Kevin Yip  @Reply  
     
3 years ago
It requires some tricky VBA to accomplish this.  You set up *unbound* textboxes (and labels) in your report.  In the report's Open event, you use VBA to pull out the field names from the crosstab query.  Put those names into the captions of the labels, and you get the column headers.  Put those field names into the *control source* of the unbound textboxes in the report detail section.  The resulting code would look something like the picture below.  In my example, I have labels named colhead0, colhead1, colhead2, etc.  And I have textboxes in the report detail named rowvalue0, rowvalue1, rowvalue2, etc.  Note how I use a For Next loop to refer to them, to avoid repetitious coding.

There are some caveats:

(1) You have to set a maximum number of columns in your report, obviously, because a report can only be so wide.  So you need to have an idea of how high the column count may be.  If the count is higher than you anticipated, some of the data will be cut off from the report, naturally.

(2) Access alphabetizes column heading names in a crosstab query.  For instance, i you have months in the columns, they will be shown alphabetically instead of chronologically: Apr, Aug, Dec, Feb, Mar, Jan, Jul, Jun, Oct, Sep, etc.  So you may need to find a way to sort the column headers (within the VBA) in the order you desire before you put them on the report.
Kevin Yip  @Reply  
     
3 years ago

Dominic Hubble OP  @Reply  
    
3 years ago
Thanks very much Kevin for your wisdom solving this - I will test soon. The maximum number of columns (products) is 10, so I should be able to format this OK. And alphabetical ordering of the products is fine too but thanks for pointing that out.
Dominic Hubble OP  @Reply  
    
3 years ago
Hi Kevin, I tried your solution with some success. The column headers work fine, but the field values do not, displaying #Name? instead of the values.

My VBA (only amended to match my camel casing) is...

Option Compare Database
Option Explicit

Private Sub Report_Open(Cancel As Integer)
' Sets the fields and values for this report dynamically, based on the columns returned by the query
    
    Dim intNumColumns As Integer
      
    
    Dim r As Recordset, f As Variant, i As Integer
    Set r = CurrentDb.OpenRecordset("ActivitiesSelectedQ_Crosstab")
    
    intNumColumns = r.Fields.Count - 1  ' remove count by one as loop begins at zero.
    
    For i = 0 To intNumColumns
       Me("colHead" & i).Caption = r.Fields(i).Name
       Me("rowValue" & i).ControlSource = r.Fields(i).Name
    Next
End Sub

Any thoughts?

Thanks, Dominic.
Dominic Hubble OP  @Reply  
    
3 years ago

Dominic Hubble OP  @Reply  
    
3 years ago

Dominic Hubble OP  @Reply  
    
3 years ago

Kevin Robertson  @Reply  
           
3 years ago
Have you set the Record Source for the Report?
Kevin Yip  @Reply  
     
3 years ago
Kevin, thanks.  Dominic, Kevin is correct.  I thought that was a matter of course, but should've mentioned it too.  The OpenRecordset in the VBA code gives you the column headers.  But the actual row data are from the record source on the report's property sheet.  That is why you see the headers and not the rows.
Kevin Yip  @Reply  
     
3 years ago
This can also be done in VBA.  Add the line below anywhere in the report's Open event:

    Me.RecordSource = "ActivitiesSelectedQ_Crosstab"
Dominic Hubble OP  @Reply  
    
3 years ago
Thanks to you both - that works fine. I thought the 'ControlSource' was fetching the data, but now I understand that it provides the 'key' to link with the query's output.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 8/8/2026 12:14:07 PM. PLT: 0s