Solved

# Calculate total value of a datagrid column vb6

Posted on 2013-02-02
Medium Priority
6,797 Views
Hi,

I'm trying to calculate the total number value of a column in a data grid and display that value in a text box using Visual Basic 6. but I'm not sure on how to achieve this ?
0
Question by:Everymancomputers
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2

LVL 6

Accepted Solution

esolve earned 2000 total points
ID: 38848218
Something like this I think (for VB6):

``````Dim rs As Recordset
Set rs = New Recordset
rs.Open "Select * From Table WHERE No= '" & txtNo.Text & "'", con, adOpenDynamic, adLockOptimistic
Set DataGrid1.DataSource = rs

Dim i
Dim Total As Double

For i = 0 To DataGrid1.ApproxCount - 1
Total =Total + DataGrid1.Columns(2).CellValue(DataGrid1.GetBookmark(i))
Next

txtTotal.Text =Total
``````
0

LVL 32

Expert Comment

ID: 38848459
if you are using the DataGrid ( and not the MSFlexGrid), I think you would be better off doing an SQL summation on the desired column, not trying to add up cells of the displayed data.
(ive never used DataGrid as found bound controls too troublesome.  the "approxCount" property raises concerns !)
0

LVL 74

Expert Comment

ID: 38853559
<calculate the total number value>
Lets be clear...
Are you wanting to "Count" the values (Number or Text)  or "Add" them (Number Only)

Does VB6 not allow Aggregate Functions?

txtYourTextBox=Dcount("YourField","YourTable")
...for a count

Or this for a sum:
txtYourTextBox=Dsum("YourField","YourTable")

JeffCoachman
0

LVL 32

Expert Comment

ID: 38853613
vb6 does not include Dsum

but should use a dbCommand object.

http://msdn.microsoft.com/en-us/library/ms675023(v=VS.85).aspx

commandText = "SELECT SUM(myColumnName) FROM myDataTable"

Set recordset = connection.Execute (CommandText, RecordsAffected, Options)
0

LVL 74

Expert Comment

ID: 38853676
robberbaron
Thanks
It's been a long time for me since last using VB6...
I just did not remember...
:-(

Jeff
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Simple Linear Regression
###### Suggested Courses
Course of the Month10 days, 10 hours left to enroll