Solved

Calculate total value of a datagrid column vb6

Posted on 2013-02-02
5
6,256 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
[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
  • Learn & ask questions
  • 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
GA Ribbon creator 9 59
Sub form showing data is being saved but cannot be displayed.. 31 66
Reoccurring Access Query 24 45
Batch convert csv to xlsx 10 53
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
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 this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

739 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