Solved

Calculate total value of a datagrid column vb6

Posted on 2013-02-02
5
6,009 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

This is about my first experience with programming Arduino.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

828 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