Solved

Calculate total value of a datagrid column vb6

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

705 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now