Connecting Tech Pros Worldwide Help | Site Map

How can I sum a column in database with a simple code?

Newbie
 
Join Date: Oct 2009
Posts: 3
#1: Oct 14 '09
t15, t14, t13, t12, t11, t10, t25, t24.... are the names of the datafield i dont know what to do. i tried many code examples but cant get it to work. i wnt to put one total to one text box. t15 is to txt15. help pls! :(
smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194
#2: Oct 14 '09

re: How can I sum a column in database with a simple code?


Say rs is your recordset. First move recordset to the required record then you may use something like:

Total = Val(rs!t15) + Val(rs!t14) + Val(rs!t13) + Val(rs!t12) + Val(rs!t11) + Val(rs!t10) + Val(rs!t25) + Val(rs!t24)

and can show Total in a textbox. If already linked textboxes with fields then simply use code like:

Total = Val(txt15) + Val(txt14) + Val(txt13) + Val(txt12) + Val(txt11) + Val(txt10) + Val(txt25) + Val(txt24)
Newbie
 
Join Date: Oct 2009
Posts: 3
#3: Oct 14 '09

re: How can I sum a column in database with a simple code?


Here is my code and its finally working. my only problem now. is how can i add another column to add. and put it to another textbox



Private Sub Form_Load()

Dim MyCon As New ADODB.Connection
Dim jai As New ADODB.Recordset
Dim sConnect As String


sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & _
App.Path & "\db1.mdb"

MyCon.Open sConnect
jai.Open "select sum(t15) from survey", MyCon, adOpenDynamic, adLockPessimistic
txt15.Text = jai.Fields(0)
jai.Close
MyCon.Close


End Sub
Reply