积极答复者
请指导计算后如何查回之前的record

问题
-
请求指导如何让我的program可以查到在DATABASE_TABLE1所输入的资料
program 遇到的问题是当user 输入数据在form1后, user要在form2加刚刚加data的quantity后 user 无法正确的查到之前的数量就好像下图
Form2
DATABASE_TABLE1
Public Sub RefreshDataDG1()
If Not dc.State = ConnectionState.Open Then
dc.Open()
End If
Dim da As New OleDb.OleDbDataAdapter("SELECT ID, Barcode as [Barcode], " & _
"Invoice as [Invoice],Qty as [Qty],PIC as [PIC],Equipment as [Equipment], Vendor as [Vendor],Description as [Description] " & _
" FROM data_info ORDER BY ID", dc)
Dim dt As New DataTable
'fill data to datatable
da.Fill(dt)
Me.DataGridView1.DataSource = dt
Me.DataGridView1.Columns("ID").Visible = False
'Close database conection
dc.Close()
End Sub计算的code
Private Sub Plus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Plus.Click
Calculate(1)
End Sub
Private Sub Subtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Subtract.Click
Calculate(0)
End Sub
Sub Calculate(ByVal Plus As Integer)
If Val(txtQty.Text) = 0 Then
Exit Sub
End If
If DataGridView1.CurrentCell.RowIndex > -1 Then
If Val(DataGridView1("ID", DataGridView1.CurrentCell.RowIndex).Value) > 0 Then
Dim cmd As New OleDb.OleDbCommand
If Not dc.State = ConnectionState.Open Then
dc.Open()
End If
cmd.Connection = dc
Dim temp As Double
Dim aqty As Double
If CBool(Plus) Then
'if plus(1)
temp = CDbl(DataGridView1("Qty", DataGridView1.CurrentCell.RowIndex).Value) + Val(txtQty.Text)
aqty = Val(txtQty.Text)
Else
'if negative(0)
temp = CDbl(DataGridView1("Qty", DataGridView1.CurrentCell.RowIndex).Value) - Val(txtQty.Text)
aqty = Val(txtQty.Text) * -1
End If
cmd.CommandText = "INSERT INTO data_info([Barcode],[Invoice],[Qty],[PIC],[Vendor],[Equipment],[Description]) " &
"VALUES('" & Me.txtBarcode.Text & "', '" & Me.txtInvoice.Text & "','" & Me.txtQty.Text & "','" & Me.txtPIC.Text & "','" & Me.txtVendor.Text & "','" & Me.txtEquipment.Text & "', '" & Me.txtDescription.Text & "')"
cmd.CommandText = "INSERT INTO tbl_stock([Barcode],[Invoice],[EquipmentQty],[PIC],[Equipment],[Vendor],[Description]) " &
"VALUES('" & CStr(DataGridView1("Barcode", DataGridView1.CurrentCell.RowIndex).Value) & "','" & Me.txtInvoice.Text & "','" & aqty & "','" & Me.txtPIC.Text & "','" & Me.txtVendor.Text & "','" & Me.txtEquipment.Text & "', '" & Me.txtDescription.Text & "')"
' "VALUES('" & DataGridView1("cid", DataGridView1.CurrentCell.RowIndex).Value & "', '" & dgvData("CheckerName", dgvData.CurrentCell.RowIndex).Value & "', '" & aqty & "', '" & Date.Now.ToString("yyyy-MM-dd HH:mm:ss") & "', '" & dgvData("CompanyName", dgvData.CurrentCell.RowIndex).Value & "', '" & Me.txtdetail1.Text & "')"
cmd.ExecuteNonQuery()
cmd.CommandText = "UPDATE data_info " & _
" SET" & _
"[Qty]='" & temp & "'" &
" WHERE [ID]=" & CDbl(DataGridView1("ID", DataGridView1.CurrentCell.RowIndex).Value)
cmd.ExecuteNonQuery()
End If
'refresh data in list
RefreshDataDG1()
loadTransactions(CInt((DataGridView1("ID", DataGridView1.CurrentCell.RowIndex).Value)))
End If
'clear form
txtQty.Text = ""
'close connection
dc.Close()
End Sub请求指导。感恩
- 已编辑 christing 2020年2月13日 10:08
答案
-
几个问题和建议:
1)
If CBool(Plus) Then
改成Plus > 0
2)
cmd.CommandText = "INSERT INTO data_info([Barcode],[Invoice],[Qty],[PIC],[Vendor],[Equipment],[Description]) " & "VALUES('" & Me.txtBarcode.Text & "', '" & Me.txtInvoice.Text & "','" & Me.txtQty.Text & "','" & Me.txtPIC.Text & "','" & Me.txtVendor.Text & "','" & Me.txtEquipment.Text & "', '" & Me.txtDescription.Text & "')"
这段代码可以不要了,因为你是更新已有记录,并非还要再data_info里边插入数据。
3)cmd.CommandText = "UPDATE data_info " & _
" SET" & _
"[Qty]='" & temp & "'" &
" WHERE [ID]=" & CDbl(DataGridView1("ID", DataGridView1.CurrentCell.RowIndex).Value)粗体部分是不是有问题?你直接把temp赋值给Qty了!假设第一次你写10,第二次写5,那么你每次点击按钮,只更新最后一次的!是不是改成(把原来的Qty的值也算上去,与temp做加减法,然后更新总的Qty)?
"[Qty]= [Qty]+'" & temp & "'" &
Reproduce your quesions with ScreenToGif is your choice.
For IIS: IIS Forum,
For WebSite of .NET: ASP.NET Forum,
For others: StackExchange.
For spam-sender or forum urgent issues, Send your Email at: forumsfeedback@microsoft.com- 已编辑 ThankfulHeartModerator 2020年2月15日 2:59
- 已建议为答案 jinglumocha 2020年2月18日 5:47
- 已标记为答案 christing 2020年2月18日 8:24
全部回复
-
你好:
总共几张表?表和表什么关系?你需要实现什么需求?期望结果是什么?实际结果又是什么?
Reproduce your quesions with ScreenToGif is your choice.
For IIS: IIS Forum,
For WebSite of .NET: ASP.NET Forum,
For others: StackExchange.
For spam-sender or forum urgent issues, Send your Email at: forumsfeedback@microsoft.com -
Hi
我有几个简单的问题需要问下。
首先,你图中画圈的两个字段之间有什么联系?
其次,你的这个DATABASE_TABLE1指的是什么?
最后,你似乎想查到之前的数据库记录,如果是这样我建议你需要做下逻辑删除(可恢复记录)。
Best Regards,
Jack
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com. -
@Thankful Heart
我有两个表
表一(Table1)和表二(Table2)
我想篇写的是当用户新加数据在同一个data时记录不会被replace
比如:table 1 有10 个苹果。当用户要在table 1 加多10个苹果
table1 = 就会出现20个苹果
用户可以在table 2 =查到之前苹果的记录和增加后的记录
如下图
Table1 的ID是auto generate 的
Table2 的 ProductNameID 是 user 选 table 1的product 时已table1 的id 为主让用户追踪质料的
- 已编辑 christing 2020年2月14日 9:44
-
几个问题和建议:
1)
If CBool(Plus) Then
改成Plus > 0
2)
cmd.CommandText = "INSERT INTO data_info([Barcode],[Invoice],[Qty],[PIC],[Vendor],[Equipment],[Description]) " & "VALUES('" & Me.txtBarcode.Text & "', '" & Me.txtInvoice.Text & "','" & Me.txtQty.Text & "','" & Me.txtPIC.Text & "','" & Me.txtVendor.Text & "','" & Me.txtEquipment.Text & "', '" & Me.txtDescription.Text & "')"
这段代码可以不要了,因为你是更新已有记录,并非还要再data_info里边插入数据。
3)cmd.CommandText = "UPDATE data_info " & _
" SET" & _
"[Qty]='" & temp & "'" &
" WHERE [ID]=" & CDbl(DataGridView1("ID", DataGridView1.CurrentCell.RowIndex).Value)粗体部分是不是有问题?你直接把temp赋值给Qty了!假设第一次你写10,第二次写5,那么你每次点击按钮,只更新最后一次的!是不是改成(把原来的Qty的值也算上去,与temp做加减法,然后更新总的Qty)?
"[Qty]= [Qty]+'" & temp & "'" &
Reproduce your quesions with ScreenToGif is your choice.
For IIS: IIS Forum,
For WebSite of .NET: ASP.NET Forum,
For others: StackExchange.
For spam-sender or forum urgent issues, Send your Email at: forumsfeedback@microsoft.com- 已编辑 ThankfulHeartModerator 2020年2月15日 2:59
- 已建议为答案 jinglumocha 2020年2月18日 5:47
- 已标记为答案 christing 2020年2月18日 8:24
-
@ThankfulHeart
感谢你回复我尝试用你的方法试试看
我想文ThankfulHeart 有经验在There is no row at position 1 or There is no row at position 0. 的error message吗?
我尝试了以下的code
Me.txtBarcode.Text = CStr(intcID)
Me.txtInvoice.Text = CStr(dt.Rows(1).Item("Invoice"))
Me.txtQty.Text = CStr(dt.Rows(2).Item("Qty"))
Me.txtPIC.Text = CStr(dt.Rows(3).Item("PIC"))
Me.txtEquipment.Text = CStr(dt.Rows(4).Item("Equipment"))
Me.txtVendor.Text = CStr(dt.Rows(5).Item("Vendor"))
Me.txtDescription.Text = CStr(dt.Rows(6).Item("Description"))
Me.txtBarcode.Text = CStr(intcID)
Me.txtInvoice.Text = CStr(dt.Rows(0).Item("Invoice"))
Me.txtQty.Text = CStr(dt.Rows(0).Item("Qty"))
Me.txtPIC.Text = CStr(dt.Rows(0).Item("PIC"))
Me.txtEquipment.Text = CStr(dt.Rows(0).Item("Equipment"))
Me.txtVendor.Text = CStr(dt.Rows(0).Item("Vendor"))
Me.txtDescription.Text = CStr(dt.Rows(0).Item("Description"))
请指教谢谢感恩
-
>>There is no row at position 1 or There is no row at position 0
这个错误通常表示没有行,请注意:.NET数组下标一律是0开始的。另外你的代码具体放在哪个事件中呢?
Reproduce your quesions with ScreenToGif is your choice.
For IIS: IIS Forum,
For WebSite of .NET: ASP.NET Forum,
For others: StackExchange.
For spam-sender or forum urgent issues, Send your Email at: forumsfeedback@microsoft.com -
@ThankfulHeart
感谢你的指教谢谢
Reproduce your quesions with ScreenToGif is your choice.
For IIS: IIS Forum,
For WebSite of .NET: ASP.NET Forum,
For others: StackExchange.
For spam-sender or forum urgent issues, Send your Email at: forumsfeedback@microsoft.com