sum a field

B

BIGRED56

What I have is 2 combo boxes with command buttond and three text boxes with
command buttons. These combos and text boxes filter a list box with records.
What I would like to do is SUM the (CostOfService) field each time their is
a filter command taking place to the list box, and have the new data show up
into a textbox each time.


In need of help
thanks...
 
B

Barry Gilbert

After each event that refills the listbox, call code like this:

Private Sub GetSum()
Dim i As Integer
Dim lngSum As Long
For i = 0 to Me.List0.ListCount - 1
lngSum = lngSum + CLng(Me.List0.ItemData(i))
Next
Me.Text1 = lngSum
End Sub

This assumes that the CostOfService column is set as the bound column in
your listbox.

Barry
 
B

BIGRED56

thanks barry,
but i have a couple of questions about the code. Should this code just be
implimented into the after update of the list box, also the list box contains
5 other fields. Does this matter in the code. And also what does sub
command "getSum" come from is that a made up command button?

Sorry I am new at this...
Thanks again...
 
B

Barry Gilbert

BIGRED56 said:
thanks barry,
but i have a couple of questions about the code. Should this code just be
implimented into the after update of the list box,

Since you have several command buttons that refilter the listbox, I
implemented this as a standalone, callable sub routine so you won't have to
put the code in multiple places. In each command button, you point to this
routine after refilling the listbox. Something like this:
Private Sub Command1_Click()
' Your current code to repopulate the listbox is here.
GetSum
End Sub


also the list box contains
5 other fields. Does this matter in the code.

The only thing that matters is that the bound column of the listbox is the
one you want to use to gather the sum. If you need a different column to be
the bound column, my code would change to:
lngSum = lngSum + CLng(Me.List0.Column(0,i)) ' You need to replace the '0'
to whichever column contains your CostOfService.
And also what does sub
command "getSum" come from is that a made up command button?

This question is addressed above.

Barry
 
B

BIGRED56

Barry thank yo uthe code works perfect, sorry I am very slow at The VB side
of access. Thanks again
 
Top