Solved

Get Sum from Field

Posted on 2009-02-10
Medium Priority
724 Views
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 sql As String
sql = "select * from Deduction where Code='" & lptxtCode & "'"
rs.Open sql, cn
With rs
If .BOF = True And _
.EOF = True Then
!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
``````
0
Question by:Whing Dela Cruz
• 6
• 4

LVL 15

Expert Comment

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

ID: 23608460
Dim sql As String
sql = sql = "SELECT SUM(Amount) as 'Total Amount' FROM Deduction WHERE code = 'CODE'"
rs.Open sql, cn
With rs
msgbox (rs("Total Amount"))
End With
Set rs = Nothing
0

Author Comment

ID: 23608754
Hello Jack ofPH,

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

``````Private Sub Command1_Click()
Dim sql As String
sql = "SELECT SUM(Amount) as 'Total Amount' FROM Deduction WHERE Amount = 'Amount'"
rs.Open sql, cn
With rs
' MsgBox (rs("Total Amount"))
text1.Text = (rs("Total Amount"))
End With
Set rs = Nothing
End Sub
``````
0

LVL 15

Expert Comment

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

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

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

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

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

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

ID: 31545431
Thanks! More Power!!
0

Featured Post

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