How do you get a Column total for a grid? 
Author Message
 How do you get a Column total for a grid?

I'm running VB6 and ADO to Jet 4.0.  I have a SQL query that returns
three rows of data to a grid.  One column is numeric; everything
displays fine.  I'm at a loss to figure out how to get a column total
(sum) for the column of numeric data.

I tried using SQL (subquery for the Sum) to do it and the best I get
is a Sum on each of the three rows.  Looks dumb.  I only want it once,
and preferably below the correct column.

I don't know how to manipulate the grid **after** it has been filled
in by the query.  I know there are methods like Grid.Columns().Value
that I could use to iterate through the data but I just don't know
how.  How do you do that and then add the sum to the displayed grid?

This one has me stumped.  Thanks, a student named Paul.

P.S., I tried the following:
     Dim i As Integer
     Dim Sum As Double

     For i = 0 To grdReportsGrid.Row - 1
         Sum = Sum + grdReportsGrid.Columns(x).Value
            Next

        MsgBox Sum

But the MsgBox tells me it returned "0".  I'm not sure about the
Columns(x) is all about.  Why (x)?  I found a similar piece of code in
the archives but can't make it fit my application.



Fri, 29 Jul 2005 13:46:21 GMT  
 How do you get a Column total for a grid?

Quote:
>I'm running VB6 and ADO to Jet 4.0.  I have a SQL query that returns
>three rows of data to a grid.  One column is numeric; everything
>displays fine.  I'm at a loss to figure out how to get a column total
>(sum) for the column of numeric data.

>I tried using SQL (subquery for the Sum) to do it and the best I get
>is a Sum on each of the three rows.  Looks dumb.  I only want it once,
>and preferably below the correct column.

>I don't know how to manipulate the grid **after** it has been filled
>in by the query.  I know there are methods like Grid.Columns().Value
>that I could use to iterate through the data but I just don't know
>how.  How do you do that and then add the sum to the displayed grid?

>This one has me stumped.  Thanks, a student named Paul.

>P.S., I tried the following:
>     Dim i As Integer
>     Dim Sum As Double

>     For i = 0 To grdReportsGrid.Row - 1
>         Sum = Sum + grdReportsGrid.Columns(x).Value
>            Next

>        MsgBox Sum

>But the MsgBox tells me it returned "0".  I'm not sure about the
>Columns(x) is all about.  Why (x)?  I found a similar piece of code in
>the archives but can't make it fit my application.

Two things.
1)  Put Option Explicit at the top of your code or else !
2) x is obviously from some example you've seen and is generally used
as an indicator that it is an index.
In your case 'i' is the index, so replace the x with i.  

Oh and put Option Explicit on the top of your code or else !  ;-)

--

Regards, Frank



Fri, 29 Jul 2005 15:30:40 GMT  
 How do you get a Column total for a grid?

Hi Frank, thanks for checking in.

Quote:
> Two things.
> 1)  Put Option Explicit at the top of your code or else !

Always!

Quote:
> 2) x is obviously from some example you've seen and is generally used
> as an indicator that it is an index.

OK, I got the following to work:

        For i = 0 To grdReportsGrid.ApproxCount - 1
            grdReportsGrid.Row = 0 + i
              Sum = Sum + grdReportsGrid.Columns(0).Value
                Next

I need .Columns(0) so I can total the column. You can see that I
increment the row number according to the "i" value. It works fine
with a couple of
caveats:

1. I get a runtime error ("invalid row number") if the recordset
scrolls more than a couple of rows past the end of the grid's visible
default.

2. The only grid property I could find that would allow me to display
the Sum was the caption.  Is there a way to insert a row into a grid
and output the Sum there?  I looked at the grid methods and didn't see
anything hopeful.

I'm 90% of the way there.  Hope you can help.  Paul



Sat, 30 Jul 2005 04:34:47 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Total of a column of Data Grid

2. Getting the total of a Table column, in MS Word using VBA

3. Agility Grid Control, Getting rid and bringing back columns

4. Agility Grid Control, Getting rid and bringing back columns

5. Checkbox in multi-column listbox or grid column?

6. Pasting a column of numbers in Excel into a grid/db grid in VB

7. Pasting a column of numbers in Excel into a grid/db grid in VB

8. Pasting a column of numbers in Excel into a grid/db grid in VB

9. Getting the Column Name of the Primary Key Column

10. databinding column totals

11. Column Totals and subtotals in Datagrid

12. COLUMN TOTALS

 

 
Powered by phpBB® Forum Software