Results 1 to 5 of 5
  1. #1
    hinchi1 is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2017
    Posts
    181

    Cross Tab problem

    I have this Cross tab query which works fine if I have the Course List set as Column Heading and the Names of the people attending the courses set as Row Headings. The issue this way is the list of courses is much greater than the number of people, so when i export the data to Excel, the sheet looks a bit out of skew. However, if I change this around to have the Course List as the Row Heading and the Names as Column Headings, the course list shows duplicate courses(which i don't want). As anyone got any ideas how to remove the duplicates? Thanks in advance. I have also added the SQL code generated from the Cross Tab Query:

    TRANSFORM Last(qCoursesAttendedReport.[Course Check]) AS [LastOfCourse Check]
    SELECT qCoursesAttendedReport.EventName AS [Courses Attended]


    FROM qCoursesAttendedReport
    GROUP BY qCoursesAttendedReport.EventName, qCoursesAttendedReport.Supervisor_FK, qCoursesAttendedReport.LineManager_FK
    ORDER BY [FirstName] & " " & [LastName]
    PIVOT [FirstName] & " " & [LastName];

    Click image for larger version. 

Name:	Skills Matrix - CrossTab.PNG 
Views:	25 
Size:	11.8 KB 
ID:	51465Click image for larger version. 

Name:	Skills Matrix.PNG 
Views:	25 
Size:	13.1 KB 
ID:	51466

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    remove the two fields you are not displaying (Supervisor_FK and LineManager_FK)

  3. #3
    hinchi1 is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2017
    Posts
    181
    Quote Originally Posted by CJ_London View Post
    remove the two fields you are not displaying (Supervisor_FK and LineManager_FK)
    Thanks for this. It worked a treat, thanks. Just need to sort out the cell wrapping when I export to Excel.

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    for that you need some code to autosize the columns in Excel.

    Pick out from this - note how it populates the spreadsheet - an alternative to using transferspreadsheet since you would otherwise need to open the excel file to format it anyway. This has been taken from one of my export functions and I've removed parts not relevant to your requirement but it should compile OK. You may not want all the steps, if you don't then just comment them out

    Code:
    'call as exportToExcel "myXtab" or whatever your query is called
    
    Function exportToExcel(qName as string)
    Dim xl As Object 'excel
    Dim wb As Object 'workbook
    Dim ws As Object 'worksheet
    
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    
    Dim c As Integer 'column iterator
    
    Set xl = CreateObject("Excel.Application")
    xl.Visible = True 'hide when code working as required
    Set wb = xl.Workbooks.Add
    
    Set ws = wb.worksheets(1)
    
    Set db=currentdb
    set rs=db.openrecordset(qName)
    
    
    
        With ws
            
                .Select
                
                '1. prevent wrapping and set font size
                .range(.columns(1), .columns(rs.Fields.Count - 1)).wraptext = False
                .cells.Font.Size = 9
                
                '2. populate header row
                For c = 0 To rs.Fields.Count - 1
                
                    ws.cells(1, c + 1).Value = rs.Fields(c).Name
                    
                Next c
                
                '3. populate data - header on top row
                .range("A2").CopyFromRecordset rs
    
    
                '4. set filter on header row
                .range(.cells(1, 1), .cells(.UsedRange.Rows.Count, rs.Fields.Count - 1)).AutoFilter 'row then column
                
                '5. set back colour on header row
                With .range(.cells(1, 1), .cells(1, rs.Fields.Count - 1)).interior
    
    
                    .Pattern = 1 'xlSolid
                    .ThemeColor = 3 'xlThemeColorDark2
                    .TintAndShade = -0.249977111117893
                    .PatternTintAndShade = 0
                    
                End With '.range
    
                '7. format columns
                For c = 0 To rs.Fields.Count - 1
                
                    Select Case rs.Fields(c).Type
                        
                        Case dbCurrency, dbDouble
                            .range(.columns(c + 1), .columns(c + 1)).NumberFormat = "#,##0.00"
                        
                        Case dbDate
                            .range(.columns(c + 1), .columns(c + 1)).NumberFormat = "m/d/yyyy"
                    
                    End Select
                    
                    .range(.columns(1), .columns(rs.Fields.Count)).EntireColumn.AutoFit 'make this the last format instruction
               
                Next c
    
           end with 'ws
    
          '9. freeze top row      
           With xl.ActiveWindow
                
               .SplitColumn = 0
               .SplitRow = 1
               .FreezePanes = True
                
          End With 'xl.ActiveWindow
    
          'change path/file name to suit
          wb.saveas fileName:= _
                CurrentProject.Path & "\Reports\" & qName & ".xlsx", FileFormat:=51, CreateBackup:=False 'xlOpenXMLWorkbook=51
    
        '12. close everything
        Set ws = Nothing
        wb.Close
        Set wb = Nothing
        xl.Quit
        Set xl = Nothing
        Set rs= Nothing
        Set db= Nothing
    
    End Function

  5. #5
    hinchi1 is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2017
    Posts
    181
    Thank you. Will have a look at this in the morning.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 03-27-2014, 07:15 AM
  2. Cross-Midnight Shift Problem
    By daniel.ru92 in forum Queries
    Replies: 10
    Last Post: 05-06-2013, 11:15 AM
  3. Problem with Cross qry
    By fcarboni in forum Access
    Replies: 1
    Last Post: 05-11-2012, 01:51 PM
  4. cross table problem
    By humanmaycry in forum Queries
    Replies: 3
    Last Post: 07-20-2011, 12:08 PM
  5. Problem with cross tab on column heading
    By pascal_22 in forum Queries
    Replies: 0
    Last Post: 12-01-2010, 08:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums