Is any error in the following code?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sreekandan
    New Member
    • Nov 2006
    • 98

    #1

    Is any error in the following code?

    Hi everybody, Im doing one project, in that i have to add all the values(Integers/Double) from one selected field of the database table.So I want the code to get the sum of that field values. I have tried with the following code
    Code:
    rs2.Open "select * from Table1 where empid='" & Text1.Tex& "'", con
             Dim k As Integer
             k = Sum(rs2.Fields(2))
    But the Compile error has occured as "Sub or Function not defined".
    So kindly reply me.
    Last edited by willakawill; Jan 31 '07, 12:28 PM. Reason: please use code tags
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Hi. The syntax would be:
    Code:
    "SELECT SUM([fieldname]) AS [mysum] FROM tablename WHERE [idfield] = idvalue"

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by sreekandan
      ...
      Code:
        rs2.Open "select * from Table1 where empid='" & [U]Text1.Tex&[/U] "'", con
        Dim k As Integer
        k = Sum(rs2.Fields(2))
      But the Compile error has occured as "Sub or Function not defined".
      So kindly reply me.
      Willakawill's response will show you how you can use SQL to produce the sum, which is likely to be much quicker and more efficient (and uses much less code). But if you prefer to use your own code to add up the values, I think your error might be in Text1.Tex& - try adding a "t" onto "Tex", and leaving a space before the "&". You have to be quite careful and precise about these little details when programming, as computers tend to take things very literally.

      I've often said, the biggest problem with computers is that they do what you tell them to, not what you want them to.

      Comment

      Working...