Question Add sum query result into a textbox

kreuznach

Member
Joined
Sep 21, 2009
Messages
5
Programming Experience
1-3
dear master, sorry if my question is dumb... :eek:
i using vbnet and accdb database
in my daylySales table i have 'date', 'salesman', 'item', and 'price' columns.
i want to sum 'price' according to a 'salesman' and 'date'
here i had query: "SELECT SUM(price) FROM daylysales WHERE date='" & txtDate.Text & "' AND salesman='" & txtSalesman.Text & "'"

my question is how to insert query result to a textbox? what syntax is used?
please if someone can give me the code...
thanks for advanced... :D
 
If you are only returning a single value with your query, you can use the ExecuteScalar method of the command object. This example is using Sql Server, so you would need to make the necessary syntax changes for use with Access.

VB.NET:
        Dim decRate As Decimal = 0

        Using con As New SqlConnection(g_strDbConnection)
            Dim cmd As New SqlCommand
            Try
                cmd.Connection = con
                cmd.CommandText = CommandType.Text
                cmd.CommandText = "Select Sum(sales).... "

                con.Open()
                decRate = CDec(cmd.ExecuteScalar)
            Catch ex As Exception
                Messagebox.Show(ex.message)
            Finally
                con.Close()
            End Try
        End Using 'con

        TextBox1.Text = FormatCurrency(decRate,2)
 

Latest posts

Back
Top