单步执行时,我的代码如何工作,但正常运行时却不行?

时间:2019-01-30 21:50:09

标签: vb.net ms-access

我正在为一个学校项目制作一个EPOS系统,除一件事情外,其他所有东西都工作正常。该代码在正常运行时不会多次运行,但是,当我逐行浏览时,它可以正常工作。该代码将条形码输入到文本框中,查询产品表以查找产品名称。然后,它获取日期,条形码通过查询销售表来组合键,以检查该天是否有该产品的先前销售。如果条目是新条目,则它将在数据库中创建新行;如果条目是新条目,则它将在销售表中查询数量,将其添加一个并用数量+1更新该行。非常感谢任何关于为什么会发生这种情况的想法。这是相关代码:

Sub txtBarcode_KeyDown(sender As Object, e As KeyEventArgs) Handles 
txtBarcode.KeyDown
    If e.KeyCode = Keys.Enter Then
        BarcodeProcess()
    end if
End Sub

Sub BarcodeProcess()
    Dim barcode As String
    barcode = txtBarcode.Text
    SoldItems(barcode)
    txtBarcode.Clear()
    txtBarcode.Select()
    txtBarcode.Update() 
End Sub

Function SoldItems(ByRef barcode As String) 'This function takes the 
barcode and makes a record in the database with the product name, amount 
sold and date sold 
    con.Close()
    con.Open()
    Dim thedate As String = String.Format("{0:dd/MM/yyyy}", DateTime.Now) 
    'Sets the date in the format day/month/year e.g. 01/02/2019
    Dim ProductBought As String = FindItem(barcode)
    'Returns the name of the the item sold
    Dim quantity As integer
    Dim test As New OleDbCommand("SELECT Product_Name FROM Sales WHERE 
    ProductID_DateSold ='" & barcode & thedate & "'", con)
    Dim numberof As New OleDbCommand("SELECT Quantity_Sold FROM Sales 
    WHERE ProductID_DateSold ='" & barcode & thedate & "'", con)
    If test.ExecuteScalar = ProductBought Then 
    'If the same product has been bought more than once in the same day 
    'then the number of items sold increases by 1
        quantity = numberof.executescalar
        quantity += 1
        Dim update As New OleDbCommand("UPDATE Sales SET Quantity_Sold =" 
        & quantity & " WHERE ProductID_DateSold ='" & barcode & thedate & 
        "'", con)
        update.ExecuteNonQuery
    Else 
    'If it is the first time the product is bought that day a new row is 
    'inserted into the database
        Dim insert As New OleDbCommand("INSERT INTO Sales 
        (Product_ID,Product_Name,Date_Sold,Quantity_Sold,
        ProductID_DateSold) VALUES ('" & barcode & "','" & ProductBought 
        & "','" & thedate & "'," & quantity + 1 & ",'" & barcode & 
        thedate & "')", con)
        Try
            insert.ExecuteNonQuery
        Catch ex As Exception
            Errors(ex.message)
        End Try
    End If
    OutputBox.Items.Insert(0, "One " & ProductBought & " has been sold.")
    OutputBox.Update()
    con.Close()
    Return nothing
End Function

Function FindItem(byVal barcode As String)
    con.Close()
    Dim name As String
    Dim da As New OleDb.OleDbCommand("SELECT Product_Name FROM Products WHERE ProductID = '" & barcode & "'", con)
    Try
        con.Open()
        name = da.ExecuteScalar
    Catch ex As Exception

    End Try
    Return name
End Function

0 个答案:

没有答案