Crosstab to display as report/form view (1 Viewer)

Kayleigh

Member
Local time
Today, 09:56
Joined
Sep 24, 2020
Messages
706
As a continuation of the attendance statistics query several days ago, I have another problem I'm grappling with.

I need to show a summary like the table below - with AM and PM as two columns for each date.
I have managed to do one column for each date but not AM/PM. I guess this can be done with two base queries and joining with a Union query.
registerSummary.png

My main problem is to create the report dynamically so depending on the date range on the form, the columns will vary. Can anyone help me with this please?
 

Attachments

  • AttendanceTEST.accdb
    4.8 MB · Views: 384

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:56
Joined
May 21, 2018
Messages
8,527
It would not be a union but a join of two queries. QryAm and QryPM joined on date and student likely.
 

isladogs

MVP / VIP
Local time
Today, 09:56
Joined
Jan 14, 2017
Messages
18,216
Something like this?
1626021298472.png


Notice how I've handled AM & PM as separate columns
I'm not sure I understand why you need two separate queries for AM & PM. That's not something I've done
To manage the issue of varying dates in the header, you need to create a dynamic crosstab report.
I can't send you the code for this as its from one of my commercial apps but there is a similar example of a dynamic crosstab report in this example app on my website: Extended File Properties - Mendip Data Systems.
Have a look and you should be able to see how its done. Any questions, get back to me

The real fun is where you want to create an editable form based on this approach 😁

1626021847906.png
 

isladogs

MVP / VIP
Local time
Today, 09:56
Joined
Jan 14, 2017
Messages
18,216
Thanks. It was a lot of work!
If you are interested, the DEMO version is on my website though the supplied data is for a previous academic year
 

Kayleigh

Member
Local time
Today, 09:56
Joined
Sep 24, 2020
Messages
706
So I tried looking at your school data analyser system but after downloading, no shortcuts were found and the relevant ACCDE file on the C Drive won't open because it needs a 32-bit version?
Can you help me out with this?
By the way did try the other system you pointed me out to - the extended properties but was too technical for me to work out what I should actually do...
 

Kayleigh

Member
Local time
Today, 09:56
Joined
Sep 24, 2020
Messages
706
I've resolved the AM/PM issue - see updated database.
Now just stuck on presenting the data dynamically....
 

Attachments

  • AttendanceTEST_2.accdb
    5.8 MB · Views: 391

isladogs

MVP / VIP
Local time
Today, 09:56
Joined
Jan 14, 2017
Messages
18,216
So I tried looking at your school data analyser system but after downloading, no shortcuts were found and the relevant ACCDE file on the C Drive won't open because it needs a 32-bit version?
Can you help me out with this?
By the way did try the other system you pointed me out to - the extended properties but was too technical for me to work out what I should actually do...
Whilst I'd be happy to supply you with a 64-bit version of the ACCDE FE used in the DEMO, its a HUGE file of around 120MB unzipped & almost 20MB zipped. That just the FE. So too big to email as an attachment.
I could upload it to a shared Dropbox folder if you wish. If so, send me your email via PM.
The rest of the files supplied with the DEMO will work in 32-bit or 64-bit.

Or you could look at the PDF of the program features first & see whether you want to proceed

Look at the report in design view and look at the naming of the controls: Lbl1=>lbl36 for the headers, chk1+>chk36 for the checkboxes & Tot1=>36 for the totals. Only the controls actually needed for the data to be displayed are made visible

The code that handles the dynamic headers in the report is shown below.
The important parts are those where the comment line starts with 'Populate ...
Remember that numbering starts at 0 so column 2 is the 3rd column

Rich (BB code):
Private Sub Report_Open(Cancel As Integer)

On Error GoTo Err_Handler
   
   '===================================================
    'Dynamic report designed to be used with Crosstab Queries
    
    'This code is especially "tuned" for crosstab queries.
    'As I like to have control over the layout, thus I have the lay-out designed first with "coded" controls.
    'Then the dynamic filling becomes very easy. The raw text I use to help with this is:
    
    'Making the column header and detail data flexible is possible,
    'but needs some VBA code in the OpenReport event.
    
    'To start doing this you need to place the fields "coded" in the report.
    'The column headings should be called "lblCol1", "lblCol2", "lblCol3", etc.
    'The "detail" fields should be called "Col1", "Col2", "Col3", etc.
    
    'The report query has two row header columns and a Total column,
    'therefore the first field is effectively column 4 (count starts at 0 so I used intI=3)
    'but this could differ for you.
    
    'The report has 36 columns so can handle up to 36 file types
    'If number of file types > 36, error 2465 occurs & the report exits.
    
    '=====================================================
   
    Dim intI As Integer
    Dim Rs As DAO.Recordset
   
    strSQL = "TRANSFORM First(tblExtPropsFileType.ExtPropValue) AS FirstOfExtPropValue" & _
        " SELECT tblExtPropsFileType.ID, tblExtPropsFileType.ExtProperty" & _
        " FROM tblExtPropsFileType" & _
        " GROUP BY tblExtPropsFileType.ID, tblExtPropsFileType.ExtProperty" & _
        " ORDER BY tblExtPropsFileType.ID" & _
        " PIVOT UCase(tblExtPropsFileType.FileExtension);"
       
    'Debug.Print strSQL
       
    Set Rs = CurrentDb.OpenRecordset(strSQL)

    'Populate headers - start at column 2
    For intI = 2 To Rs.Fields.Count - 1
       Me("Lbl" & intI - 1).Caption = Rs.Fields(intI).Name
       Me("Lbl" & intI - 1).Visible = True
         
    Next intI

    'Populate checkbox controls
    For intI = 2 To Rs.Fields.Count - 1
        Me("chk" & intI - 1).ControlSource = Rs.Fields(intI).Name
        Me("chk" & intI - 1).Visible = True
    Next intI
   
   
    'Populate totals
    'each filled value = -1 (true) so do -SUM to get total
    For intI = 2 To Rs.Fields.Count - 1
        Me("Tot" & intI - 1).ControlSource = "=-SUM([" & Rs.Fields(intI).Name & "])"
        Me("Tot" & intI - 1).Visible = True
    Next intI
       
Exit_Handler:
    Exit Sub

Err_Handler:
    If Err = 2465 Then 'too many data columns for report (max = 36)
        N = DCount("FileExtension", "tblExtPropsFileType", "ExtProp='Name'")
        If N > 36 Then
            FormattedMsgBox "Extended property summary data has been saved for " & N & " file types         " & _
            "@The summary report can only show results for the first 36 file types         " & vbNewLine & vbNewLine & _
            "Delete unwanted data from the table 'tblExtPropsFileType' before running this report        @", vbCritical, "Too much data!!"
            Cancel = True
            Exit Sub
        End If
    Else
        strProc = "Report_Open"
        MsgBox "Error " & Err.Number & " in " & strProc & " procedure: " & Err.Description
        Resume Exit_Handler
    End If
End Sub

Sorry but I haven't got time to look at your database at the moment.
 

Kayleigh

Member
Local time
Today, 09:56
Joined
Sep 24, 2020
Messages
706
Hi I did get there in the end!!
I now have another crosstab query that I'm trying to find the best way to implement.
I have ONE table of student info including all medical consent. The client would like all the student names as column headings (not too hard) and each consent as another row (ie the values will be true/false). How to do this?
I can think of a method to do this programmatically but I'm sure it is possible in SQL. Would appreciate all suggestions
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:56
Joined
May 21, 2018
Messages
8,527
You would have to give us a snap shot of the table design. It is hard to say without. can you provide a couple of rows and blank out any student names and any private info?
 

Kayleigh

Member
Local time
Today, 09:56
Joined
Sep 24, 2020
Messages
706
consent sample.png
I would like to pivot this view - having students as columns and consent types as rows.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:56
Joined
May 21, 2018
Messages
8,527
To do this you are going to first have to make a big union query.
Here is my example
tblConsent tblConsent

IDStudentNameOutingsPhotosVideos
1​
John
Yes​
Yes​
Yes​
2​
Patty
Yes​
Yes​
No​
3​
Betty
Yes​
No​
Yes​
4​
Paul
No​
Yes​
No​
5​
Susie
Yes​
No​
Yes​
Now union all the consent fields
Code:
SELECT tblConsent.ID, tblConsent.StudentName, tblConsent.Outings AS Consent, "Outings" AS Category
FROM tblConsent
UNION
SELECT tblConsent.ID, tblConsent.StudentName, tblConsent.Photos AS Consent, "Photos" AS Category
FROM tblConsent
UNION SELECT tblConsent.ID, tblConsent.StudentName, tblConsent.Videos AS Consent, "Videos" AS Category
FROM tblConsent
ORDER BY 1, 2, 4;
qryConsent qryConsent

IDStudentNameConsentCategory
1​
John
-1​
Outings
1​
John
-1​
Photos
1​
John
-1​
Videos
2​
Patty
-1​
Outings
2​
Patty
-1​
Photos
2​
Patty
0​
Videos
3​
Betty
-1​
Outings
3​
Betty
0​
Photos
3​
Betty
-1​
Videos
4​
Paul
0​
Outings
4​
Paul
-1​
Photos
4​
Paul
0​
Videos
5​
Susie
-1​
Outings
5​
Susie
0​
Photos
5​
Susie
-1​
Videos
Now crosstab on that
Code:
TRANSFORM First(qryConsent.[Consent]) AS FirstOfConsent
SELECT qryConsent.[Category]
FROM qryConsent
GROUP BY qryConsent.[Category]
PIVOT qryConsent.[StudentName];

result
qryConsent_Crosstab qryConsent_Crosstab

CategoryBettyJohnPattyPaulSusie
Outings
Yes​
Yes​
Yes​
No​
Yes​
Photos
No​
Yes​
Yes​
Yes​
No​
Videos
Yes​
Yes​
No​
No​
Yes​
You need to change the format on the "consent" field to show yes no instead of 0, -1.

If you have to see checkboxes then I would do that in a tabular form. It can be done in a query but requires code the alter the querydef.
 

June7

AWF VIP
Local time
Today, 00:56
Joined
Mar 9, 2014
Messages
5,470
How many students? Limit of 255 fields (columns) in query.
 

Kayleigh

Member
Local time
Today, 09:56
Joined
Sep 24, 2020
Messages
706
@June7 I guess I can filter by class if necessary

Looks great @MajP - will give it a go and let you know how I get on.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:56
Joined
May 21, 2018
Messages
8,527
See example. I was wrong. To show checkboxes you click on that column in design view and go to properties. In the lookup tab select "Display Control" and pick checkboxes. As pointed out you may be limited to 254 (1 for name) students as a column limit.
 

Attachments

  • Consents.accdb
    476 KB · Views: 342
Last edited:

Users who are viewing this thread

Top Bottom