Results 1 to 2 of 2
  1. #1
    NavyDave is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2017
    Posts
    1

    Dynamic Crosstab Query Report Average Calculation

    Hello,

    I am new to vba, but have been using Access for a while. I googled for the code that I finally got to work with my db that totals the columns at the bottom. I need it to average this number because it is for students semester grades. I added a "Count(*)" text box to my Report to get the number of records and then I use that text box as my average divider. That is crude, but it works if there are scores entered. if there is a zero, then this throws the average off.

    Im sure there is slicker way to do this with code, but im still learning. if not, can I somehow change my formula to exclude any null values when dividing for the average?

    here is my code that I pulled off of a website and adapted it for my db. the cheesy average calculation is highlighted in orange
    Option Compare Database
    ' Constant for maximum number of columns EmployeeSales query would
    ' create plus 1 for a Totals column. Here, you have 9 employees.
    Const conTotalColumns = 29
    ' Variables for Database object and Recordset.
    Dim dbsReport As DAO.Database
    Dim rstReport As DAO.Recordset


    ' Variables for number of columns and row and report totals.
    Dim intColumnCount As Integer
    Dim lngRgColumnTotal(1 To conTotalColumns) As Long
    Dim lngReportTotal As Long

    Private Sub InitVars()

    Dim intX As Integer
    ' Initialize lngReportTotal variable.
    lngReportTotal = 0

    ' Initialize array that stores column totals.
    For intX = 1 To conTotalColumns
    lngRgColumnTotal(intX) = 0
    Next intX
    End Sub

    Private Function xtabCnulls(varX As Variant)

    ' Test if a value is null.
    If IsNull(varX) Then
    ' If varX is null, set varX to 0.
    xtabCnulls = 0
    Else
    ' Otherwise, return varX.
    xtabCnulls = varX
    End If
    End Function

    Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
    ' Put values in text boxes and hide unused text boxes.

    Dim intX As Integer
    ' Verify that you are not at end of recordset.
    If Not rstReport.EOF Then
    ' If FormatCount is 1, put values from recordset into text boxes
    ' in "Detail" section.
    If Me.FormatCount = 1 Then
    For intX = 1 To intColumnCount
    ' Convert Null values to 0.
    Me("Col" + Format(intX)) = xtabCnulls(rstReport(intX - 1))
    Next intX

    ' Hide unused text boxes in the "Detail" section.
    For intX = intColumnCount + 2 To conTotalColumns
    Me("Col" + Format(intX)).Visible = False
    Next intX
    ' Move to next record in recordset.
    rstReport.MoveNext
    End If
    End If

    End Sub

    Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)

    Dim intX As Integer
    Dim lngRowTotal As Long
    ' If PrintCount is 1, initialize rowTotal variable.
    ' Add to column totals.
    If Me.PrintCount = 1 Then
    lngRowTotal = 0

    For intX = 2 To intColumnCount
    ' Starting at column 2 (first text box with crosstab value),
    ' compute total for current row in the "Detail" section.
    lngRowTotal = lngRowTotal + Me("Col" + Format(intX))
    ' Add crosstab value to total for current column.
    lngRgColumnTotal(intX) = lngRgColumnTotal(intX) + Me("Col" + Format(intX))
    Next intX

    ' Put row total in text box in the "Detail" section.
    Me("Col" + Format(intColumnCount + 1)) = lngRowTotal
    ' Add row total for current row to grand total.
    lngReportTotal = lngReportTotal + lngRowTotal
    End If
    End Sub

    Private Sub Detail_Retreat()
    ' Always back up to previous record when "Detail" section retreats.
    rstReport.MovePrevious
    End Sub

    Private Sub PageHeaderSection_Format(Cancel As Integer, FormatCount As Integer)

    Dim intX As Integer

    ' Put column headings into text boxes in page header.
    For intX = 1 To intColumnCount
    Me("Head" + Format(intX)) = rstReport(intX - 1).Name
    Next intX
    ' Make next available text box Totals heading.
    Me("Head" + Format(intColumnCount + 1)) = "Totals"
    ' Hide unused text boxes in page header.
    For intX = (intColumnCount + 2) To conTotalColumns
    Me("Head" + Format(intX)).Visible = False
    Next intX
    End Sub

    Private Sub Report_Close()

    On Error Resume Next
    DoCmd.Close acForm, "Academic Status Form"

    ' Close recordset.
    rstReport.Close

    End Sub

    Private Sub Report_NoData(Cancel As Integer)
    MsgBox "No records match the criteria you entered.", vbExclamation, "No Records Found"
    rstReport.Close
    Cancel = True
    End Sub

    Private Sub Report_Open(Cancel As Integer)
    ' Create underlying recordset for report using criteria entered in
    ' EmployeeSalesDialogBox form.

    Dim intX As Integer
    Dim qdf As QueryDef
    Dim frm As Form
    ' Set database variable to current database.
    Set dbsReport = CurrentDb
    Set frm = Forms!AcademicStatusForm
    ' Open QueryDef object.
    Set qdf = dbsReport.QueryDefs("qryCrosstabDynamic")
    ' Set parameters for query based on values entered
    ' in EmployeeSalesDialogBox form.
    qdf.Parameters("Forms!AcademicStatusForm!cboSemest er") _
    = frm!cboSemester
    qdf.Parameters("Forms!AcademicStatusForm!cboClassL ist") _
    = frm!cboClassList
    ' Open Recordset object.
    Set rstReport = qdf.OpenRecordset()

    ' Set a variable to hold number of columns in crosstab query.
    intColumnCount = rstReport.Fields.Count


    End Sub

    Private Sub ReportFooter_Print(Cancel As Integer, PrintCount As Integer)


    Dim intX As Integer
    ' Put column totals in text boxes in report footer.
    ' Start at column 2 (first text box with crosstab value).
    For intX = 2 To intColumnCount
    Me("Tot" + Format(intX)) = lngRgColumnTotal(intX) / [Text104]
    Next intX
    ' Put grand total in text box in report footer.
    Me("Tot" + Format(intColumnCount + 1)) = lngReportTotal
    ' Hide unused text boxes in report footer.
    For intX = intColumnCount + 2 To conTotalColumns
    Me("Tot" + Format(intX)).Visible = False
    Next intX
    End Sub

    Private Sub ReportHeader_Format(Cancel As Integer, FormatCount As Integer)
    ' Move to first record in recordset at the beginning of the report
    ' or when the report is restarted. (A report is restarted when
    ' you print a report from Print Preview window, or when you return
    ' to a previous page while previewing.)
    rstReport.MoveFirst
    'Initialize variables.
    InitVars
    End Sub

  2. #2
    NTC is offline VIP
    Windows 7 64bit Access 2013
    Join Date
    Nov 2009
    Posts
    2,392
    Wrong approach in my opinion. Assuming grades of students are in stored a properly normal table (i.e. 1 grade per record and not side-by-side excel-style in a single record): then you create an Aggregate Query using its Avg feature to result in a query record set of 1 record per student with the Avg value. Save this query named AverageQ.

    Then create a select query with all the Student info you seek (individual grades) and write it into a temp table: PreCrossTab.

    Append AverageQ into PreCrossTab.

    PreCrossTab should then contain all the data you seek to have in your cross tab results.

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

Similar Threads

  1. Dynamic CrossTab Report from Query
    By ZJGMoparman in forum Programming
    Replies: 1
    Last Post: 08-20-2015, 10:54 AM
  2. Average calculation in query >0
    By lschuh in forum Access
    Replies: 2
    Last Post: 10-15-2013, 08:57 AM
  3. Dynamic crosstab report sorting headings & dlookup
    By chrisangk in forum Programming
    Replies: 9
    Last Post: 01-14-2013, 02:37 PM
  4. Calculation Help, Either Query or Crosstab
    By mweisler20 in forum Queries
    Replies: 1
    Last Post: 06-30-2012, 01:44 PM
  5. report average calculation
    By ZipDoc in forum Reports
    Replies: 1
    Last Post: 01-28-2010, 09:08 PM

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