PIE Chart and live data on the form.
Students' Exam Scores in five different Subjects are recorded in the table: tbl_Students. A sample Table image is given below:
The student’s name from the current record on the form is used as a criterion in a query to filter that student’s exam record. These filtered records are then used as the source data for the Pie Chart displayed on the same form. A sample form with the Pie Chart is shown below:
Out of the Total five Exam subjects, if any one of the records becomes current on the form, the Pie Chart should display the marks of all five subjects, along with their percentage of the maximum Total marks, on the same form.
This is the tricky part: we cannot directly design a Graph Chart on a form in normal view to filter the query that serves as the data source.
Data Source Queries.
To prepare the data for the chart, we need three simple queries to filter and organize the students’ exam records. In addition, a temporary table is required to store the total of all obtainable maximum marks for each subject (for example, 100 × 5 = 500). This value will be used to calculate the percentage of marks obtained in the chart. The image of the temporary table is shown below:
The Three-Part Approach.
We will divide the task into three parts so that the procedure is easy to understand:
Part-A: Create tbl_Students and tmp_MaxMarks.
Part-B: Design the Form frm_Students for tbl_Students and create three simple Queries to prepare the data for Charts.
Part-C: Create a PIE Chart on the frm_Students Form.
Part-A:
Create a table with the same structure in the first image at the top, and name it tbl_Students. You may ignore the two empty fields in the table design. As illustrated, the table contains exam scores for three students, each evaluated in five subjects, with a maximum of 100 marks per subject.
Key in the sample data into the tbl_Students Table.
Create a small table with two fields, as shown in the image above, and save it as tbl_MaxMarks.
Create a single record with the sample data, shown in the image above.
Part-B:
Design a form in Columnar format based on tbl_Students, leaving enough space on the right side of the form to accommodate the PIE Chart. You can use the Form Wizard to create the Form quickly. Once completed, save the form as frm_Students.
We will create three Queries before working with the Form.
Create the following Queries by copying and pasting the SQL String into the new Query’s SQL Editing Window and saving it with the name as indicated:
Query-1: StudentsQ
SELECT [Student] & " " & [Subject] AS [Desc], tbl_Students.Marks FROM tbl_Students WHERE (((tbl_Students.Student)=[forms]![frm_Students]![student]));
Query-2: MaxMarksQ
SELECT tmp_MaxMarks.Desc, [MaxMarks]-DSum("Marks","StudentsQ") AS Max_Marks FROM tmp_MaxMarks;Query-3: UnionPieChart
SELECT StudentsQ.* FROM StudentsQ UNION ALL SELECT MaxMarksQ.* FROM MaxMarksQ;
Open frm_Students in Design View.
Create two TextBoxes below the existing fields.
Click the first TextBox and press F4 to display the Property Sheet.
Change the Name Property value to Obtained.
Type the following expression into the Control Source Property:
=DSum("Marks","StudentsQ")Change the Caption Property Value of the Child Label of the Textbox to Total Obtained.
Click on the second Textbox to select it.
Write the following expression into the Control Source Property:
=[Obtained]/DLookUp("MaxMarks","tmp_MaxMarks")Change the Format Property Value to Percentage.
Change the Caption Property value of the Child Label of the Text Box to % of the Total.
Save the Form and open it in Normal View.
Let us review what we did so far.
Created the main Table tbl_Students and designed the Form frm_Students.
While the frm_Students is active, the StudentsQ data is filtered using the current student's name on the Form as a criterion.
Second Query (MaxMarksQ):
This query sums up the total marks obtained by each student from StudentsQ and compares it against the overall maximum marks (500) stored in tmp_MaxMarks. By subtracting the student’s obtained total from the maximum, we calculate the marks lost.
On the PIE Chart, this difference is represented as a percentage of the total. For example, if the chart shows 10% lost, it means the student secured 90% of the aggregate marks out of 500.
Third Query: UnionPieChart combines the StudentsQ source data and MaxMarksQ queries together for the PIE Chart Source Data.
Part-C:
Our PIE Chart needs to be displayed on the frm_Students Form. However, there’s a challenge:
-
The form must remain Open in Normal View so that the selected student’s record can filter the data in StudentsQ.
-
At the same time, a chart object can be created or embedded on a Form, only when in Design View.
This is why we need a small trick to accomplish the task.
Let’s assume the form frm_Students is open, displaying John’s record (the first student). His exam data is already filtered and available through StudentsQ. With this in place, follow these steps to create and embed the PIE Chart into the form:
While keeping the frm_Students in Form View, open a new Form in Design View.
Enable Use Control Wizard Button on the Toolbar above, and select Insert Chart Tool from the Controls group.
Draw a Chart Object in rectangular shape, large enough to show the formatting (Value Labels, Title, etc.), similar to the sample image above.
Select Queries from the View group of the Wizard control and select UnionPieChart from the displayed query list.
Click Next to proceed to the next screen, and select the >> button to select all the displayed fields for the chart.
Click on the PIE Chart Icon to select it and click the Next button twice, then type the following as a heading on the Title control:
Max. Marks Each=100, 500 Total
Select No, don't display a Legend, then click Finish.
Double-click the PIE Chart to change to Edit Mode.
Right-click on an empty area of the chart to display the context menu.
Select Chart Options from the displayed menu.
Select the Data Labels Tab, and put checkmarks in Category Name, Values, and Percentage.
Click OK to close the control.
We will reduce the size of the PIE slice labels to smaller font sizes.
Click one of the PIE slice Labels. This action selects all Labels together.
Change the Font size to 12 points, using the Font/Size Tool above.
The completed design of the PIE Chart is given below.
Click outside the Chart Object in the Form to exit Chart-Edit Mode, but the chart will still be in the selected state.
Right-click the Chart to display the shortcut menu, and select Copy to copy the Chart Object to the Clipboard.
Save the form with a name and close it if you would like to keep it safe. But we don’t need it anymore.
Now, change the frm_Students into Design View.
Click in the Detail Section of the Form to make it current.
Right-click on the Detail Section and select Paste from the displayed menu.
Drag the PIE Chart and place it on the right side of the data fields.
Click the top-left corner of the Form (to select Form-level Properties) and display the Property Sheet (F4) of the Form.
Select the Event Tab of the Property Sheet and click on the On Current property.
Select the Event Procedure from the drop-down list, click the Build (...) Button to open the VBA editing window.
Write the following statement in the middle of the Form_Current() Event Procedure:
Me.Refresh
When completed, the procedure will look like the following:
Private Sub Form_Current() Me.Refresh End SubWhen you move records on the Form, the Form_Current() event procedure will update the record set with the change on the StudentsQ as well.
Close the VBA editing window.
Save the frm_Students Form and open it in Normal View.
The first five records on the Form belong to student John. If you move one record at a time, you will not find any difference on the Chart up to the fifth record, because the values of all five records are shown on the PIE Chart. The sixth to tenth records belong to the second student, and the 11th record onwards belongs to the third student. You may type 6 or 11 on the Record Navigation Control to quickly display other students' marks on the PIE Chart.


























