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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create a function to SUM a column in a MSFlexGrid 1

Status
Not open for further replies.

homesick

Programmer
Dec 5, 2001
55
0
0
CA
Season's greetings all!!

Just wondering how i would create a function to sum a column in a MSFlexGrid? I wanted to have a coomadnbutton to perform the function and a textbox to display it. Is this possible and can anyone help?


simon
 
This should be what u need:

----------------------------
Private Sub Command1_Click()
Text1.Text = TotalColumn(FlexGrid, 2)

End Sub

Private Function TotalColumn(Grid As MSHFlexGrid, ByVal ColIndex As Integer) As Integer
Dim R As Long
Dim Total As Integer

For R = 0 To Grid.Rows - 1
If IsNumeric(Grid.TextMatrix(R, ColIndex)) Then
Total = Total + Grid.TextMatrix(R, ColIndex)
End If
Next R

TotalColumn = Total

End Function

--------------------------

HTH.

~Mike
Any man willing to sacrifice liberty for security deserves neither liberty nor security.

-Ben Franklin
 
Thanks for the help but it doesn't seem to be working....it gives me the error "Compile Error: ByRef argument type mismatch" ....what does that mean?"


Private Sub Command22_Click()
Text1.Text = TotalColumn(FlexGrid, 15) ' 15 is my column i would like to sum

End Sub

Private Function TotalColumn(Grid As MSFlexGrid, ByVal ColIndex As
Integer) As Integer
Dim R As Long
Dim Total As Integer

For R = 0 To Grid.Rows - 1
If IsNumeric(Grid.TextMatrix(R, ColIndex)) Then
Total = Total + Grid.TextMatrix(R, ColIndex)
End If
Next R

TotalColumn = Total

End Function
 
The only thing I can see right away is in your function header:

Private Function TotalColumn(Grid As MSFlexGrid, etc...)
'Change to:
Private Function TotalColumn(Grid AS MSHFlexGrid, etc...)

Also, when you call the function, i.e. "Text1.Text = TotalColumn(FlexGrid, 15)", make sure that 'FlexGrid' is the name of your grid.

At any rate, a ByRef argument type mismatch error is blaming the grid part of the function call, but the rest looks ok...try checking these and see what happens.

~Mike


Any man willing to sacrifice liberty for security deserves neither liberty nor security.

-Ben Franklin
 
Almost there but now it's returning a value if "0"....calculation doesn't seem to be working...
 
Try changing this line:
If IsNumeric(Grid.TextMatrix(R, ColIndex)) Then
Total = Total + Grid.TextMatrix(R, ColIndex)

to this:
If IsNumeric(CInt(Grid.TextMatrix(R, ColIndex))) Then
Total = Total + CInt(Grid.TextMatrix(R, ColIndex))

You may have spaces or something else that's causing IsNumeric() to return false. Put a breakpoint on the line that increments Total and make sure it's actually getting there, it sounds like it's not...


Any man willing to sacrifice liberty for security deserves neither liberty nor security.

-Ben Franklin
 
it was actually my TotalColumn = Total....i misspelled "column"....working good....but now i'm sometimes getting "Overflow" error...looks like the number might be too large...any suggestions?
 
Integers can hold values from about 32000 to -32000. Try changing Total to Long (about +/- 4 million, as well as the return value from the function:

Private Function TotalColumn(Grid As MSFlexGrid, ByVal ColIndex As Integer) As Long
Dim R As Long
Dim Total As Long

For R = 0 To Grid.Rows - 1
If IsNumeric(Grid.TextMatrix(R, ColIndex)) Then
Total = Total + Grid.TextMatrix(R, ColIndex)
End If
Next R

TotalColumn = Total

End Function

If you still get an overflow error you may need to change to Single or Double.

HTH

~Mike
Any man willing to sacrifice liberty for security deserves neither liberty nor security.

-Ben Franklin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top