Get numerical total

1. Junior Member
Join Date
Dec 1969
Posts
3

## Get numerical total

I would like to add the total from the records that is displayed.<BR><BR>I can&#039;t use SUM becuase it only gets the total from the database:<BR>I am trying to get the total of two fields multiplied.<BR>(Field1)1*(Field2)2 = (subtotal)2<BR>(Field1)3*(Field2)3 = (subtotal)9<BR>Final Total = 11<BR><BR><BR>

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Are you displaying all the records?

In a loop?<BR><BR>If so it&#039;s dirt simple:<BR><BR>&#060;%<BR>... get your recordset ...<BR><BR>total = 0<BR><BR>Do Until RS.EOF<BR> subtotal = RS("field1") * RS("field2")<BR> total = total + subtotal<BR> ... display fields for this record including that subtotal value ...<BR> ...<BR> RS.MoveNext<BR>Loop<BR>... now you can display the total value ...<BR>%&#062;<BR>

3. Junior Member
Join Date
Dec 1969
Posts
3

## RE: Are you displaying all the records?

Yes, it is a loop, and no not all of the records are being displayed, but shouldn&#039;t matter should it?

4. Senior Member
Join Date
Dec 1969
Posts
6,476

## RE: Are you displaying all the records?

Cant sql do it?<BR><BR>SELECT Field1, Field2, Field3, [Field2]+[Field3] AS MyTotal FROM MyTable<BR><BR>Then loop through records.<BR>rs("MyTotal")<BR><BR>Not sure it makes any diference.<BR>Just I&#039;ve always done most of my calcs in the sql.<BR><BR>Any difference performance wise?

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## Sure...either one...

But you still have to do<BR> total = total + subtotal<BR>in the loop unless you are prepared to make an entirely separate query just to get it.<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## I meant all the records in the recordset

SURELY you aren&#039;t skipping records in the recordset???<BR><BR>Why would you do that?<BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
6,476

## RE: Are you displaying all the records?

Makes no difference because you are doing the totaling in the loop<BR>so it is performed for each record.<BR><BR>As bill said this will work.<BR><BR>Do Until RS.EOF <BR> subtotal = RS("field1") * RS("field2") &#039;This adds the "Current Record only<BR> <BR>total = total + subtotal &#039;This is running total of all the records.<BR> ... display fields for this record including that subtotal value ... <BR> ... <BR> RS.MoveNext <BR>Loop <BR>

8. Junior Member
Join Date
Dec 1969
Posts
3

## RE: I meant all the records in the recordset

Not skipping anything. Just filtering.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•