[英]get sum of a column and a value inputted in textbox in vb.net
i have a textbox named AddU.text, i also have a msaccess table field named Quantity, what I want is that when i input a value in AddU.Text and click the add button, the value i entered will be automatically add to the existing value in Quantity. 我有一个名为AddU.text的文本框,也有一个名为Quantity的msaccess表字段,我想要的是,当我在AddU.Text中输入一个值并单击添加按钮时,我输入的值将自动添加到现有的数量中的值。 I keep searching for solution but can't find the right one. 我一直在寻找解决方案,但找不到合适的解决方案。 Can anyone help me? 谁能帮我? Heres my current codes: 这是我当前的代码:
Dim cmd As New OleDb.OleDbCommand
If Not conn.State = ConnectionState.Open Then
conn.Open()
End If
Try
cmd.Connection = conn
cmd.CommandText = "UPDATE BC_Inventory SET [Addition]='" + AddU.Text + "'," + _
"[Date_Updated]='" + DateU.Text + "',[Time_Updated]='" + TimeU.Text + "',[Updated_By]='" + UpdatedBy.Text + "'" + _
"WHERE [Item]='" + com_ItemU.Text + "'"
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error")
Finally
conn.Close()
End Try
actually i still have no codes for it, i keep trying the codes i found in researching but nothing helps thats why i deleted it. 实际上,我仍然没有代码,我一直尝试尝试在研究中发现的代码,但是没有任何帮助,这就是为什么我删除了它。
I suspect that your quantity-field is a field in your table? 我怀疑您的数量字段是表中的字段吗? This is the changed SQL-Query. 这是更改后的SQL查询。
Dim cmd As New OleDb.OleDbCommand
If Not conn.State = ConnectionState.Open Then
conn.Open()
End If
Try
' SQL-Query with Database Update on Field Quantity = Quantity + AddU.Text
Dim strSQL As String = "" & _
"UPDATE BC_Inventory " & _
"SET [Addition] = '" & AddU.Text & "', " & _
" [Quantity] = CDbl([Quantity]) + CDbl(" & CDbl(AddU.Text.Replace(",", ".")) & "), " & _
" [Date_Updated] = '" & DateU.Text & "', " & _
" [Time_Updated] = '" & TimeU.Text & "', " & _
" [Updated_By] = '" & UpdatedBy.Text & "' " & _
"WHERE [Item] = '" & com_ItemU.Text & "' "
' For Debugging
' MsgBox(strSQL)
cmd.Connection = conn
cmd.CommandText = strSQL
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error")
Finally
conn.Close()
End Try
Edit 1 : A " sign was double in the line "WHERE [Item] ..."
编辑1 : "WHERE [Item] ..."
行中的符号是“两倍"WHERE [Item] ..."
Edit 2 : Changed Convert(DOUBLE, Quantity)
to Convert(DOUBLE, [Quantity])
编辑2 : Convert(DOUBLE, Quantity)
更改为Convert(DOUBLE, [Quantity])
Edit 3 : Remark for debugging. 编辑3 :备注调试。
' For Debugging
' MsgBox(strSQL)
Edit 4 : Added the missing )
to Convert(DOUBLE, '" & CDbl(AddU.Text) & "', "
right at the end. 编辑4 :最后将缺少的)
添加到Convert(DOUBLE, '" & CDbl(AddU.Text) & "', "
。
Edit 5 : changed )'
to ')
编辑5 :将)'
更改为')
Edit 6 : Now with CDec()
instead of Convert(Double, )
编辑6 :现在使用CDec()
而不是Convert(Double, )
Edit 7 : CDbl(AddU.Text.Replace(",", "."))
编辑7 : CDbl(AddU.Text.Replace(",", "."))
Edit 8 : Removed the ' around the CDbl(AddU.Text) 编辑8 :删除CDbl(AddU.Text)周围的'
Edit 9 : replaced the CDec
with CDbl
编辑9 :用CDbl
替换CDec
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.