Re: Sum Array from Database
2,1,0,1,1,5,2... So this is one record with 7 fields/table columns? And you want to sum the values of each column across all records (rows)?
If so, you can use the SUM function, like: SELECT SUM(column_name) FROM table_name
More info here: http://www.w3schools.com/sql/sql_func_sum.asp
Re: Sum Array from Database
Hi i think i cannot perform a SUM function because it has commas (,)
2,1,0,1,1,5,2 << 1 record in 1 field commas are included.
In my code i split it with comma as the delimiter.
so its like based on my code
myArr(0) = 2
myArr(1) = 1
myArr(2) = 0
and so on. now i need to loop on the recordset and sum it there along the way. i don't know how to loop it where it will put the values on a temp file and movenext to the recordset.
Re: Sum Array from Database
Try this
Code:
Dim I As Long
Dim J As Long
Dim lngSums(6) As Long
Dim strData() As String
For I = 0 To rs.RecordCount - 1
strData= Split(rs!FieldFromDbase, ",")
For J = 0 To 6
lngSums(J) = lngSums(J) + CLng(strData(J))
Next J
rs.Movenext
Next I
I didn't test this, just wrote it here.
Re: Sum Array from Database
Thank you for the quick response. I really appreciate it. I will try it and let you know thank you.
Re: Sum Array from Database
works great i just modified the code adding " If RS.EOF Then Exit For"
Dim I As Long
Dim J As Long
Dim lngSums(6) As Long
Dim strData() As String
Code:
For I = 0 To rs.RecordCount - 1
If rs.EOF Then Exit For
strData= Split(rs!FieldFromDbase, ",")
For J = 0 To 6
lngSums(J) = lngSums(J) + CLng(strData(J))
Next J
rs.Movenext
Next I
Thank you very much. Problem solved.
Re: Sum Array from Database
You're welcome! ;)
Please take time to mark the thread Resolved. You can do so by going to the Thread Tools menu, which is above your original post, and choosing Mark Thread Resolved.