How to sum the records of a field?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ali Rizwan
    Banned
    Contributor
    • Aug 2007
    • 931

    How to sum the records of a field?

    Hi all,
    I have a field in an access database name Cost i want to find the sum of all amount.
    Database name = EXP
    Using ADODC
    VB6

    I have a number of three adodcs on same form.
    Plz give me the solution, using my information.

    Tablename = EXP
    DataBase name = Expenses
    ADODC name = ADATA
    Field name = Cost

    Thanx
  • nev
    Contributor
    • Oct 2007
    • 251

    #2
    Dim cval as Integer = 0

    For i as integer = 0 to EXP.Count - 1
    cval += val(EXP.Item("C ost", i).Value)
    Next

    MsgBox(cval.ToS tring)

    Comment

    • debasisdas
      Recognized Expert Expert
      • Dec 2006
      • 8119

      #3
      Try to use SQL for the purpose.

      Comment

      • Ali Rizwan
        Banned
        Contributor
        • Aug 2007
        • 931

        #4
        Originally posted by nev
        Dim cval as Integer = 0

        For i as integer = 0 to EXP.Count - 1
        cval += val(EXP.Item("C ost", i).Value)
        Next

        MsgBox(cval.ToS tring)
        Hi,
        Thanx for the answer but in which language you write it i want a code for vb6.
        This code is not executing, and occuring somee errors on it.
        Thanx

        Comment

        • Ali Rizwan
          Banned
          Contributor
          • Aug 2007
          • 931

          #5
          Originally posted by debasisdas
          Try to use SQL for the purpose.
          Sorry debasis,
          but i don't know how to apply that code.
          I tried but i said later i m recieving some errors.
          Plz made an example code or that code
          Thanx

          Comment

          • dreamcode
            New Member
            • Oct 2007
            • 2

            #6
            Originally posted by Ali Rizwan
            Hi all,
            I have a field in an access database name Cost i want to find the sum of all amount.
            Database name = EXP
            Using ADODC
            VB6

            I have a number of three adodcs on same form.
            Plz give me the solution, using my information.

            Tablename = EXP
            DataBase name = Expenses
            ADODC name = ADATA
            Field name = Cost

            Thanx
            Hi,
            Try the SQL query like,

            rs.open "select sum(cost) from exp"

            i hope this would help you.
            All the best

            Comment

            • Ali Rizwan
              Banned
              Contributor
              • Aug 2007
              • 931

              #7
              Originally posted by dreamcode
              Hi,
              Try the SQL query like,

              rs.open "select sum(cost) from exp"

              i hope this would help you.
              All the best
              I m sorry.
              What is cost means in the query can you plz explain it to me or made an example in vb6.
              My app is delaying to the customer because of this only.
              Thanx i ll very thak full to every body.
              thanx again

              Comment

              • muddasirmunir
                Contributor
                • Jan 2007
                • 284

                #8
                asalam o alikum
                did you try this query in
                "select sum(fieldname) as total from talblename "

                replase filedname and tablename according to your tables
                where fieldname=the field you want to count
                talblename=the name of table weher you have a field
                try this query




                Originally posted by Ali Rizwan
                I m sorry.
                What is cost means in the query can you plz explain it to me or made an example in vb6.
                My app is delaying to the customer because of this only.
                Thanx i ll very thak full to every body.
                thanx again

                Comment

                • Ali Rizwan
                  Banned
                  Contributor
                  • Aug 2007
                  • 931

                  #9
                  Originally posted by muddasirmunir
                  asalam o alikum
                  did you try this query in
                  "select sum(fieldname) as total from talblename "

                  replase filedname and tablename according to your tables
                  where fieldname=the field you want to count
                  talblename=the name of table weher you have a field
                  try this query
                  Walaikumusallam

                  I m doing this query like this

                  Code:
                  Dim ras As Integer
                  
                  ras = "select sum(Cost) as total from Exp "
                  MsgBox ras
                  and the msgbox shows "select sum(Cost) as total from Exp " in message

                  Thanx

                  Comment

                  • DeBees Consults
                    New Member
                    • Apr 2016
                    • 1

                    #10
                    ' I assume that your data base is connected and working fine


                    Dim SumCost As Double ' Declare as a global variable

                    Create a button name btnSUM and Caption it &Sum

                    Privte Sub btnSUM_Click()

                    SumCost = "0.00"

                    Do until ADATA.Recordset .EOF = True
                    If ADATA.RecordSet .Fields("Cost") <> "" then
                    SumCost = SumCost + ADATA.RecordSet .Fields("Cost")
                    End if
                    ADATA.Recordset .MoveNext
                    Loop
                    MsgBox("The sum of fields Cost = " SumCost)
                    ' You can do whatever you want to do with SumCost here

                    ' Possible Error: Where fields Cost is empty

                    ' I hope this Suffices your purpose
                    ' you can contact me on studybies@gmail .com
                    End Sub

                    Comment

                    Working...