Solved

Calculate total value of a datagrid column vb6

Posted on 2013-02-02
5
6,124 Views
Last Modified: 2013-02-05
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
Comment
Question by:Everymancomputers
  • 2
  • 2
5 Comments
 
LVL 6

Accepted Solution

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

Dim rs As Recordset
Set rs = New Recordset
rs.CursorLocation = adUseClient
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

Open in new window

0
 
LVL 32

Expert Comment

by:Robberbaron (robr)
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

by:Jeffrey Coachman
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

by:Robberbaron (robr)
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

by:Jeffrey Coachman
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question