Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Calculate Values in a Datagrid.

Status
Not open for further replies.

1x2z3

Programmer
Sep 18, 2003
39
0
0
ZA
I`am entering values in a datagrid.

How can i calculate all the values of prices of items added to the datagrid, to show me the sum of all the items prices added together.
 
I'll assume that you are adding and/or modifying prices in the grid. Usually the easiest way is to recompute the sum using some SQL after each change to the grid as in the following example.
Code:
Private Sub DBGrid1_AfterUpdate()
Dim SQL                         As String
Dim rs                          As New ADODB.Recordset
Dim cn                          As New ADODB.Connection
SQL = "Select SUM(Price) As SP From myTable Where ... "
cn.Open "Provider= ... Some Connection String ... "
rs.CursorLocation = adUseClient
rs.Open SQL, cn
If IsNull(rs![SP]) Then
    lblPrice.Caption = "$0.00"
Else
    lblPrice.Caption = Format(rs![SP], "$###,##0.00")
End If
Set rs = Nothing
Set cn = Nothing

End Sub
where "lblPrice" is a label control that you use to display the price.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top