Public Class frmItem
    Public ItemID As Integer = 0
    Private Sub frmItem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If ItemID = 0 Then
            cleartext(Me)

            cbo_fill(cboBrand, "Brand", "BrandID", "tblbrand")
            cbo_fill(cboParts, "Parts", "PartsID", "tblparts")
            cbo_fill(cboSupplier, "Supplier", "SupplierId", "tblsupplier")
            'cbo_fill(cboLocation, "Location", "LocationID", "tbllocation")
            'cbo_fill(cboCompSet, "ComputerSet", "CompSetID", "tblcompset")
        End If


    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            If txtBarcode.Text = "" Or txtDescription.Text = "" Then
                MsgBox("Fields are required.", MsgBoxStyle.Exclamation)
            Else
                'sql = "SELECT * FROM `tblitems` " & _
                '    " WHERE Barcode='" & txtBarcode.Text & "'"
                'reloadtxt(sql)

                'If dt.Rows.Count > 0 Then
                '    MsgBox("Item is already exist!", MsgBoxStyle.Exclamation)
                'Else
                sql = "Select * From tblitems WHERE ItemID = " & ItemID

                sqladd = "INSERT INTO `tblitems` (`Barcode`, `BrandID`, `Description`, `PartsID`, `RecievedDate`, `Quantity`, `Price`,  `Status`,`Supplier`) " &
                         " VALUES ('" & txtBarcode.Text & "'," & cboBrand.SelectedValue & ",'" & txtDescription.Text & "'," & cboParts.SelectedValue & ",Date(Now()),0,'" & txtprice.Text & "','Good','" & cboSupplier.Text & "')"

                sqledit = "UPDATE `tblitems` SET `Barcode`='" & txtBarcode.Text & "', `BrandID`='" & cboBrand.SelectedValue & "', `Description`='" & txtDescription.Text & "', `PartsID`=" & cboParts.SelectedValue & ",Quantity='1',Price='" & txtprice.Text & "',Supplier='" & cboSupplier.Text & "' WHERE ItemID=" & ItemID

                save_or_update(sql, sqladd, sqledit, "Item has been updated in the database.", "New Item has been saved in the database.")

                Call frmItem_Load(sender, e)

            End If
            'End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        cleartext(Me)

        cbo_fill(cboParts, "Parts", "PartsID", "tblparts")
        cbo_fill(cboBrand, "Brand", "BrandID", "tblbrand")
        cbo_fill(cboSupplier, "Supplier", "SupplierId", "tblsupplier")
        'cbo_fill(cboLocation, "Location", "LocationID", "tbllocation")
        'cbo_fill(cboCompSet, "ComputerSet", "CompSetID", "tblcompset")
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
    Private Sub cboParts_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cboParts.KeyPress, cboBrand.KeyPress
        e.Handled = True
    End Sub

    Private Sub cboSupplier_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cboSupplier.KeyPress
        e.Handled = True
    End Sub

    Private Sub txtBarcode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBarcode.TextChanged
        sql = "SELECT * FROM `tblitems` " &
                  " WHERE Barcode='" & txtBarcode.Text & "'"
        reloadtxt(sql)

        If dt.Rows.Count > 0 Then
            MsgBox("Item is already exist!", MsgBoxStyle.Exclamation)
            Call btnNew_Click(sender, e)
        End If
    End Sub
End Class