VBA to add Totals to subform (1 Viewer)

bodders24

New member
Local time
Today, 02:58
Joined
Oct 3, 2016
Messages
24
I have built a procedure to dynamically rebuild a subform and underlying table based on a crosstab query. This is so that it can show various combinations of categories (rows) and monthly totals (columns). I would like to have a total row appearing on the form and to use VBA to achieve this. The subform is based on a select query from the calculated table.

I have read several posts, and I think this is the code to do so:

Code:
    Set qdf = dbs.QueryDefs("qselBudgetXT")
    qdf.Properties("TotalsRow") = True
     qdf.Fields(2).Properties("AggregateType") = 0
     qdf.Fields(3).Properties("AggregateType") = 0
     qdf.Fields(4).Properties("AggregateType") = 0
     qdf.Fields(5).Properties("AggregateType") = 0
     qdf.Close

However, the subform shows a Totals row, but no actual totals in the cells in that row. I can add them manually but that defeats the object.

Subform.jpg


Does anyone have any thoughts or suggestions. All help gratefully received.

Thanks

Bodders
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:58
Joined
Sep 21, 2011
Messages
14,317
Aren't you setting them to zero?
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:58
Joined
Sep 21, 2011
Messages
14,317
Never used them, but just try setting to any value as a debug option?


 
Last edited:

CJ_London

Super Moderator
Staff member
Local time
Today, 02:58
Joined
Feb 19, 2013
Messages
16,618
my guess is you have turned off warnings. The totals row property doesn’t exist until you create it. See bottom of this link
 

bodders24

New member
Local time
Today, 02:58
Joined
Oct 3, 2016
Messages
24
Never used them, but just try setting to any value as a debug option?


Interestingly the query opened on its own has the Total row with the correct values. Only on the subform - opened separately - it does not. Strange
 

CJ_London

Super Moderator
Staff member
Local time
Today, 02:58
Joined
Feb 19, 2013
Messages
16,618
Only on the subform
do you mean the subform, or are you using the query as the source object to the subform control
 

bodders24

New member
Local time
Today, 02:58
Joined
Oct 3, 2016
Messages
24
Here's the query opened on its own with subtotals showing:

Query.jpg


And here is the subform with the above query as its Recordsource (no totals showing):


I cannot work out why this should be.
 

bodders24

New member
Local time
Today, 02:58
Joined
Oct 3, 2016
Messages
24
Look at post #7? :(
Changing

Code:
qdf.Fields(5).Properties("AggregateType") = 0

to

Code:
qdf.Fields(5).Properties("AggregateType") = acAggregateSum

actually produces the wrong answer in the query. It gives the average.
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:58
Joined
Sep 21, 2011
Messages
14,317
Changing

Code:
qdf.Fields(5).Properties("AggregateType") = 0

to

Code:
qdf.Fields(5).Properties("AggregateType") = acAggregateSum

actually produces the wrong answer in the query. It gives the average.
So try them all. :(
Not the first time the MS help could be wrong. :)
 

bodders24

New member
Local time
Today, 02:58
Joined
Oct 3, 2016
Messages
24
my guess is you have turned off warnings. The totals row property doesn’t exist until you create it. See bottom of this link
Warnings are definitely on
 

CJ_London

Super Moderator
Staff member
Local time
Today, 02:58
Joined
Feb 19, 2013
Messages
16,618
how about a comment on the link I provided? plus post #8 - sounds like you are using a form (per post 9), not the query. To use the query the subform sourceobject would be

query.nameofquery

your code is modifying the querydef, not the form
 

bodders24

New member
Local time
Today, 02:58
Joined
Oct 3, 2016
Messages
24
Thanks for the responses. Having tried everything suggested above, here is the current situation.

The query which is the SourceObject for the datasheet subform produces a totals row and individual total rows exactly as it should, using AggregateType = 0.

The problem is the subform. I can add the AggregateType property to each control, but it does not show when the form is displayed. It also does not allow me to set a TotalsRow property for the form.

Opening the subform separately allows me to manually create the totals for each cell in the Totals row, but the dynamic VBA process deletes these controls when it is rebuilt, and the totals are lost, despite the VBA recreating them:

Code:
        If HasProperty(ctl, "AggregateType") = False Then
            Set obj = ctl
            obj.Properties.Append dbs.CreateProperty("AggregateType", dbLong, acAggregateSum)
        Else
            ctl.Properties("AggregateType") = acAggregateSum
        End If

This code produces an error:

Code:
If HasProperty(frm, "TotalsRow") = False Then
    Set obj = frm
    obj.Properties.Append dbs.CreateProperty("TotalsRow", dbBoolean, True)
End If
 

CJ_London

Super Moderator
Staff member
Local time
Today, 02:58
Joined
Feb 19, 2013
Messages
16,618
Having tried everything suggested above
so what happened when you used the query as the subform control source object?

works for me
image_2023-05-17_112255889.png
 
Last edited:

CJ_London

Super Moderator
Staff member
Local time
Today, 02:58
Joined
Feb 19, 2013
Messages
16,618
you haven't shown your full code, perhaps you are not saving the query def, or not refreshing the subform control
 

Users who are viewing this thread

Top Bottom