?
Solved

Get Sum from Field

Posted on 2009-02-10
10
Medium Priority
?
724 Views
Last Modified: 2012-05-06
how to get sum from the field?
the code below shows to save the data.
Can you help me how to get the sum of the !amount?
Public Sub save_Deduction(cn As ADODB.Connection, lptxtCode As String, Description As String, Amount As String)
Dim rs As ADODB.Recordset
Dim sql As String
Set rs = New ADODB.Recordset
    sql = "select * from Deduction where Code='" & lptxtCode & "'"
    rs.LockType = adLockOptimistic
    rs.CursorType = adOpenKeyset
    rs.Open sql, cn
    With rs
        If .BOF = True And _
            .EOF = True Then
            .AddNew
            !Code = lptxtCode
        End If
        !Code = Trim(txtCode.Text)
        !Description = Trim(txtDescription.Text)
        !Amount = Trim(txtAmount.Text)
        .Update
        .Close
    End With
    Set rs = Nothing
End Sub

Open in new window

0
Comment
Question by:Whing Dela Cruz
  • 6
  • 4
10 Comments
 
LVL 15

Expert Comment

by:JackOfPH
ID: 23608455
Execute this query... It will return the Sum of the amount...

sql = "SELECT SUM(Amount) as 'Total Amount' FROM Deduction WHERE code = 'CODE'
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 23608460
Dim rs As ADODB.Recordset
Dim sql As String
Set rs = New ADODB.Recordset
    sql = sql = "SELECT SUM(Amount) as 'Total Amount' FROM Deduction WHERE code = 'CODE'"
    rs.LockType = adLockOptimistic
    rs.CursorType = adOpenKeyset
    rs.Open sql, cn
    With rs
        msgbox (rs("Total Amount"))
    End With
    Set rs = Nothing
0
 

Author Comment

by:Whing Dela Cruz
ID: 23608754
Hello Jack ofPH,

I think i'm very wrong with my application.. Please modify

Private Sub Command1_Click()
Dim rs As ADODB.Recordset
Dim sql As String
Set rs = New ADODB.Recordset
    sql = "SELECT SUM(Amount) as 'Total Amount' FROM Deduction WHERE Amount = 'Amount'"
    rs.LockType = adLockOptimistic
    rs.CursorType = adOpenKeyset
    rs.Open sql, cn
    With rs
       ' MsgBox (rs("Total Amount"))
       text1.Text = (rs("Total Amount"))
    End With
    Set rs = Nothing
End Sub

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:JackOfPH
ID: 23608861
Replace this:

sql = "SELECT SUM(Amount) as 'Total Amount' FROM Deduction WHERE Amount = 'Amount'"


With this one...

sql = "SELECT SUM(Amount) as 'Total Amount' FROM Deduction"
0
 

Author Comment

by:Whing Dela Cruz
ID: 23608988
During Execution, An error Message appear " Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another. then if i click debug
rs.open sql, cn is being HighligHted. what does it mean?
Is my code above is correct ? What shall i do?
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 23609337
Private Sub Command1_click()

msgbox(GetTotalAmount)

end sub

Private Function GetTotalAmount() as decimal

sql = "SELECT SUM(Amount) as 'Total Amount' FROM Deduction"

GetTotalAmount = cn.executeScalar(sql)

end sub
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 23609410
Private Sub Command1_click()

msgbox(GetTotalAmount(cn))

end sub

Private Function GetTotalAmount(Byval cn as Connection) as decimal

sql = "SELECT SUM(Amount) as 'Total Amount' FROM Deduction"

GetTotalAmount = cn.executeScalar(sql)

end sub
0
 

Author Comment

by:Whing Dela Cruz
ID: 23609588
Hello JackofPh,

the command is not accepted in my computer it will become red when i execute...
Private Function GetTotalAmount(Byval cn as Connection) as decimal

0
 
LVL 15

Accepted Solution

by:
JackOfPH earned 2000 total points
ID: 23618344



Private Sub Command1_click()

msgbox(GetTotalAmount(cn))

end sub

Private Function GetTotalAmount(byval cn As ADODB.Connection) as decimal

sql = "SELECT SUM(Amount) as 'Total Amount' FROM Deduction"

GetTotalAmount = cn.executeScalar(sql)

end sub
0
 

Author Closing Comment

by:Whing Dela Cruz
ID: 31545431
Thanks! More Power!!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question