Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sum across columns??

Status
Not open for further replies.

uguimess

Technical User
Jul 29, 2009
24
0
0
CA
I have a subform in datasheet view. When entering a value in any of several "SurveyCount" textboxes, I need to update the sum of all those textboxes in another "CalculatedTotal" textbox within that record. How on earth is this done???

Note: Because users can add custom fields, I don't know at design time how many of the qualifying fields will be included in the sum.

-----------------------------------------
Where would we be if we didn't try?
 
I tried this and it worked once, but seems to be in an endless loop which lets me navigate from field to field in the same record, but otherwise freezes me out.

Code:
Private Sub Form_AfterUpdate()    
    If Screen.ActiveControl.Name <> "txtTtl" Then Call UpdateMyTotal
End Sub

Private Sub UpdateMyTotal()
    Dim iTotal As Integer
    Dim ctl As Control
   
    iTotal = 0
    For Each ctl In Me.Controls
        If ctl.ControlType = acTextBox And ctl.Tag <> 0 Then
            If Not Trim(ctl.Value & "") & "" = "" And IsNumeric(ctl.Value) Then iTotal = iTotal + ctl.Value
        End If
    Next ctl
    Me.txtTtl = iTotal
End Sub

-----------------------------------------
Where would we be if we didn't try?
 
Can't you just use on the fields after update event a refernce to whatever the calcuated field is called

After Update:

me.calculatedfieldname.refresh
(whatever the name of calcuated field is)
 
Thanks Knicks.

Because of a thypo in your reply, I am not sure what you are recommending.

I hear the term "calculated field" at times, but I am not sure precisely what it means; whether a field with some sort of formula associated with it, or just a field that holds the result of some calculation. So where would I perform the desired calculation? I am drawing a blank here.

The Sourceobject for the subform is a query of an underlying temp table. Does the Total field need to be in the temp table too or can it just be in the form?

-----------------------------------------
Where would we be if we didn't try?
 
found another thread that clued me in to using Form_BeforeUpdate. It works now.

-----------------------------------------
Where would we be if we didn't try?
 
Ahhhh!

Not totally working. You have to go to a new record to see the updated calculation. How can I get it to update when focus changes to another field within the same record???

-----------------------------------------
Where would we be if we didn't try?
 
solved. can't remember how.

-----------------------------------------
Where would we be if we didn't try?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top