I tried a statement that is very close to the one you sent as the control
source of ExtendedPriceSum in the subform footer. This statement below is
similar to yours except that if the RecordCount is not = 0, then it sums
[ExtendedPrice] because it is not yet summed..
=IIf(Forms!EditDeleteInvoice!InvoiceDetailsSubEdit.Form.RecordsetClone.Recor
dCount=0,0,Sum([ExtendedPrice]))
This works. The subform shows the correct amount in ExtendedPriceSum and the
InvoiceTotal on the main form shows the correct amount. I used the
following statement as the control source of InvoiceTotal:
=IIf(IsError(EditDog.Form!txtTotalSalePrice),0,EditDog.Form!txtTotalSalePric
e) +
IIf(IsError(InvoiceDetailsSubEdit.Form!ExtendedPriceSum),0,InvoiceDetailsSub
Edit.Form!ExtendedPriceSum).
Now a problem happens when the user attempts to add a new record to the
subform by putting a value in the control, cboChargeCode. When this is
attempted, the user gets the message: "Field cannot be updated", even
though the subform does allow the record to be added. How can I keep this
message from appearing?? This has only started happening since I put the
line:
=IIf(Forms!EditDeleteInvoice!InvoiceDetailsSubEdit.Form.RecordsetClone.Recor
dCount=0,0,Sum([ExtendedPrice])) as the control source for
ExtendedPriceSum.
When I had: = Sum([ExtendedPrice]) as the control source the message didn't
appear. I wonder if the reason it appears now is that I am referring to
the underlying recordset simultaneously in the control source of the total
control on the subform??? Was hoping that the property, RecordsetClone,
would help in making Access think that the recordset was not being used by
two processes simultaneously. What do you think?
Joan
Marshall Barton said:
Sorry, Joan, I was off in my thinking. Here's an expression
that I actually tested ;-)
=IIf([nameofsubform].[Form].[RecordsetClone].[RecordCount]=0,0,[nameofsubfor
m].[Form].[ExtendedPriceTotal])
I had already tried what you suggested, but if there are no records in
the subform, no total shows in either the subform or on the main form.
Joan wrote:
Could anyone tell me whether or not the SUM() function is available to
use
in VBA? As part if an IIF statement , I would like one of the arguments
to
sum the values in a field on a subform. Below is the line of code that I
am
trying to use in the OnCurrent event of the subform. ExtendedPriceSum is
the control with the total in the form footer.
Private Sub Form_Current()
Me.ExtendedPriceSum = IIf(IsError([ExtendedPriceSum]), 0,
Sum([ExtendedPrice]))
End Sub
When I run my form, I get the following message: "Sub or Function not
defined." and Sum in the above line is highlighted when I debug. How do
I
sum ExtendedPrice in my code?
:
The Sum function is not available in the VBA environment, It
only exists in the SQL and control source expression
environments.
Put a text box named ExtendedPriceTotal in the subform's
footer section. Set its expression to:
=Sum(ExtendedPrice)
Then, your ExtendedPriceSum text box on the main form can
display the sum using a text box by using the expression:
=IIf(IsError(ExtendedPriceTotal, 0, ExtendedPriceTotal)