Friday, December 14, 2007

How to get the total amount in fields?

How to get the total amount in fields?


When we need to get the total amount, normally we need to use the for..loop to add it one by one.

For example:


<%

Set conn = Server.CreateObject("Adodb.Connection")

conn.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("db1.mdb")

Set rs = Server.CreateObject("Adodb.Recordset")

strMember="Jack"

strSQL ="SELECT * FROM Sales WHERE Buyer='" & strMember & "';" 'Select all sales transaction that make by a member.

rs.Open strSQL,conn,1,3

Do while Not rs.eof

vntTotal = vntTotal + rs.fields("Price")

rs.MoveNext

Loop

rs.Close

Set rs = Nothing

Response.Write("The total payment is " & vntTotal & "
")

%>

Or, we use the SQL command to return to the total of value:

<%

Set conn = Server.CreateObject("Adodb.Connection")

conn.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("db1.mdb")

Set rs = Server.CreateObject("Adodb.Recordset")

strMember="Jack"

strSQL ="SELECT Sum(*) As TotalAmount FROM Sales WHERE Buyer='" & strMember & "';" 'Select all sales transaction that make by a member; and sum the price that the member need to pay.

rs.Open strSQL,conn,1,3

If Not rs.EOF Then

Response.Write("The total payment is " & rs.fields("TotalAmount") & "
")

End If

rs.Close

Set rs = Nothing

%>

No comments: