开发者

How to create an Access crosstab query with totals for the columns AND the rows?

I want my query result to look like this:

          Person1 Person2 Person3 Person4    Total 
Status1         2       4       7       3      16
Status2         0       1       0       3      4
Status3         0       0       0       0      0
Status4         0       1       3       0      4
Total           2       6       10      6      24

I'm able to get everything except that bottom row with:

TRANSFORM Count(personName) 
SELECT status, Count(status) AS Total
FROM table1 
GROUP BY status
P开发者_运维问答IVOT personName

I found something about using a UNION to tack on the last row, but I can't seem to quite get that right. Seems like this should be a common activity.


You'd basically have to run your query twice - once to get the data and then a second time to provide the aggregates. If you're set on doing this, make the first query to return data its own object. Then make another query to aggregate the first one another object. Create a final third query object to combine the two using a UNION as you mentioned.

Although I have to say I don't really recommend this. It sounds like you're trying to force the SQL to generate something that's really presentational information (i.e. it doesn't belong in the same dataset).


There is actually a simple solution to this issue. Once you have designed your crosstab query, go into design mode within the query and select "Totals" in the Records section on the Home tab. Then you can select the Sum or Count etc....

Here is a link that gives steps: http://office.microsoft.com/en-us/access-help/display-column-totals-in-a-datasheet-HA001233062.aspx


I've been looking for a solution too. Could not find one either except writing a query based on the crosstab and then summing that one and adding in to the bottom in a union query. Since I try to do all SQL statements from inside a form (more manageable to deploy) I do not like this approach: writing or refilling a Querydef/view from code etc.

If you display the results in a subform on your form, you might do the following:

below the subform, and another subform short enough to hold only 1 record.

Bind the controls in the form to a function as follows:

control1 = fnADOSum(yourCrosstabfield1, yourCrosstabSQL) 

Public Function fnADOSum(fldName As String, strInputSQL As String) As Double
    On Error GoTo ERRHANDLER

    Dim RS1 As ADODB.Recordset
    Dim cnn As ADODB.Connection
    Dim StrSQL As String
    Dim dblRunTot As Double


    Set RS1 = New ADODB.Recordset
    RS1.CursorLocation = adUseServer
    Set cnn = CurrentProject.Connection

    dblRunTot = 0

    With RS1
    .Open strInputSQL, cnn, adOpenForwardOnly, adLockReadOnly
        If Not .EOF And Not .BOF Then
            .MoveFirst
            Do Until .EOF
            dblRunTot = dblRunTot + Nz(.Fields(fldName).Value, 0)
            .MoveNext
            Loop
        End If
    .Close
    End With

    fnADOSum = dblRunTot

    'CLEAN UP:
    cnn.Close
    Set RS1 = Nothing
    Set cnn = Nothing


    EXITHANDLER:
    Exit Function

    ERRHANDLER:
    '' your own error handling proc
    '' LogError err.Number, err.Description


End Function


Lydia wrote: "There is actually a simple solution to this issue. Once you have designed your crosstab query, go into design mode within the query and select "Totals" in the Records section on the Home tab. Then you can select the Sum or Count etc...."

Going into the design mode did not work for me:

  • I ran the query.
  • Then went to the Home tab
  • Selected Totals in the Records section
  • The label "Totals" appeared at the bottom of the Crosstab query results, but no actual totals yet.
  • Clicked on the empty cell to the right of the Totals label.
  • An arrow appeared, and I chose "Sum".

[I am using Access 2013]


Found after much trial and error...

To toggle the totals rows ON when you enter a form or sub-form you can add the following VBA in the form's code:

Private Sub YourFormName_Enter()
    If Application.CommandBars.GetPressedMso("RecordsTotals") = False Then
        Application.CommandBars.ExecuteMso "RecordsTotals"
    End If
End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜