Imports System.Data
Imports System.Data.OleDb
Public Class frm_LotBlanks
Dim Conn As OleDbConnection
Private dataset As DataSet
Private cmd As OleDbCommand
Private objDataAdapter As OleDbDataAdapter
Private objDataTable As DataTable
Dim Dttd As String = ""
Private Sub frm_LotBlanks_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Conn = New OleDbConnection(conStr)
Conn.Open()
Dttd = Today.Date.ToShortDateString
ReadData()
FillDataGridView()
End Sub
Private Sub ReadData()
Dim sql As String = ""
sql = "SELECT Store.StoreName, Location.LocName, Lot.LotNo FROM "
sql &= "((Lot LEFT JOIN Location ON Lot.LocID = Location.LocID) "
sql &= "LEFT JOIN Store ON Location.StoreID = Store.StoreID) WHERE Lot.LotStatus = '" & "Y" & "' ORDER BY Lot.LotNo ASC"
cmd = New OleDbCommand(sql, Conn)
Dim adapter As New OleDbDataAdapter(cmd)
dataset = New DataSet()
adapter.Fill(dataset, "cust")
End Sub
Private Sub FillDataGridView()
dgvLotBlank.DataSource = dataset.Tables("cust")
dgvLotBlank.AlternatingRowsDefaultCellStyle.BackColor = Color.Bisque
dgvLotBlank.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
With dgvLotBlank
.Columns("StoreName").DisplayIndex = 0
.Columns(0).HeaderText = "คลังสินค้า"
.Columns(0).Width = 100%
.Columns(0).ReadOnly = True
.Columns("LocName").DisplayIndex = 1
.Columns(1).HeaderText = "ชื่อที่เก็บ"
.Columns(1).Width = 100%
.Columns(1).ReadOnly = True
.Columns("LotNo").DisplayIndex = 2
.Columns(2).HeaderText = "ล็อตสินค้าที่ว่าง"
.Columns(2).Width = 100%
.Columns(2).ReadOnly = True
End With
End Sub
Private Sub btnPrint2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint2.Click
End Sub
Private Sub frm_Location_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
Conn.Close()
End Sub
End Class
Code (VB.NET)
Imports System.Data.OleDb
Imports System.Data
Public Class frm_receiveproduct
Dim Conn As OleDbConnection
Private dataset1 As DataSet
Private dataset2 As DataSet
Private dataset3 As DataSet
Private dataset4 As DataSet
Private cmd As OleDbCommand
Private objDataAdapter As OleDbDataAdapter
Private objDataTable As DataTable
Private rowtxtid As Integer = 0
Private rowtxtidpro1 As Integer = 0
Private rowtxtidpro2 As Integer = 0
Private rowtxtidpro3 As Integer = 0
Dim tmpPid As String = ""
Dim Dttd As String = ""
Dim CurrentRow As Integer = 0
Private Sub frm_receiveproduct_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Conn = New OleDbConnection(conStr)
Conn.Open()
Dttd = Today.Date.ToShortDateString
Dim isFind As Boolean = False
Dim ds As New DataSet
Dim sql As String = "SELECT * from Store "
If isFind = True Then
ds.Tables("ib").Clear()
End If
Dim da As New OleDbDataAdapter(sql, Conn)
da.Fill(ds, "ib")
If ds.Tables("ib").Rows.Count <> 0 Then
isFind = True
With cbbStore
.DataSource = ds.Tables("ib")
.DisplayMember = "StoreName"
.ValueMember = "StoreID"
End With
Else
isFind = False
cbbStore.DataSource = Nothing
End If
cbbLocation.Enabled = False
cbbLot.Enabled = False
btnDelete.Enabled = False
NewNB()
FormatdgvReceiveproduct()
End Sub
Private Sub NewNB()
Dim maxNum As Integer
Dim SQL As String = "SELECT max(Tid) FROM TransactionStore "
cmd = New OleDbCommand(SQL, Conn)
cmd.Connection = Conn
Cmd.CommandType = CommandType.Text
cmd.CommandText = SQL
Try
maxNum = Cmd.ExecuteScalar()
Catch ex As NullReferenceException
maxNum = 0
Finally
maxNum = maxNum + 1
txtidr.Text = maxNum.ToString("00000")
End Try
End Sub
Private Sub txtid_enter(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtid.KeyPress
Dim KeyAscii As Short = Asc(e.KeyChar)
Dim strSQL As String = ""
strSQL = "SELECT * FROM Employee WHERE EmployeeCode = '" & Me.txtid.Text & "'"
cmd = New OleDbCommand(strSQL, Conn)
Dim adapter As New OleDbDataAdapter(cmd)
dataset1 = New DataSet()
adapter.Fill(dataset1, "Emy")
rowtxtid = dataset1.Tables("Emy").Rows.Count
If KeyAscii = 13 Then
If txtid.Text = "" Then
txtname.Text = ""
MessageBox.Show("กรุณาใส่รหัสพนักงาน ด้วยครับ", "No ID", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
If rowtxtid <= 0 Then
txtname.Text = ""
MessageBox.Show("รหัสพนักงานไม่ถูกต้อง", "ข้อมูลไม่ถูกต้อง", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
txtname.Text = dataset1.Tables("Emy").Rows(0)("EmployeeName")
End If
End If
End If
End Sub
Private Sub txtidpro_enter(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtidpro.KeyPress
Dim KeyAscii As Short = Asc(e.KeyChar)
Dim sql As String = ""
sql = "SELECT Item.ItemCode, Brand.BrandName, Item.ItemName, "
sql &= "UnitOfMeasure.NameUnitOfMeasure, Item.UnitCost, Item.UnitPrice FROM "
sql &= "((Item LEFT JOIN Brand ON Item.BrandCode = Brand.BrandCode ) LEFT JOIN UnitOfMeasure ON Item.UmCode = UnitOfMeasure.UmCode) WHERE Item.ItemCode = '" & Me.txtidpro.Text & "'"
cmd = New OleDbCommand(sql, Conn)
Dim adapter1 As New OleDbDataAdapter(cmd)
dataset1 = New DataSet()
adapter1.Fill(dataset1, "items")
rowtxtidpro1 = dataset1.Tables("items").Rows.Count
sql = "SELECT * FROM Stock WHERE ItemCode = '" & Me.txtidpro.Text & "'"
cmd = New OleDbCommand(sql, Conn)
Dim adapter2 As New OleDbDataAdapter(cmd)
dataset2 = New DataSet()
adapter2.Fill(dataset2, "st")
rowtxtidpro2 = dataset2.Tables("st").Rows.Count
If KeyAscii = 13 Then
If txtidpro.Text = "" Then
Cleartxtpro()
MessageBox.Show("กรุณาใส่รหัสสินค้า ด้วยครับ", "No Item Code", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
If rowtxtidpro1 <= 0 Then
Cleartxtpro()
MessageBox.Show("รหัสสินค้าไม่ถูกต้อง", "ข้อมูลไม่ถูกต้อง", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
If rowtxtidpro2 <= 0 Then
datapro()
MessageBox.Show("สินค้ายังไม่เคยจัดเก็บในคลัง กรุณาเลือกคลังสินค้า", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Information)
cbbStore.Enabled =True
Else
datapro()
cbbLot.Text = dataset2.Tables("st").Rows(0)("LotNo")
Dim sqlLoc As String = ""
sqlLoc = "SELECT Location.LocName FROM Lot LEFT JOIN Location ON Lot.LocID = Location.LocID WHERE Lot.LotNo = '" & Me.cbbLot.Text & "'"
cmd = New OleDbCommand(sqlLoc, Conn)
Dim adapter3 As New OleDbDataAdapter(cmd)
dataset3 = New DataSet()
adapter3.Fill(dataset3, "ln")
cbbLocation.Text = dataset3.Tables("ln").Rows(0)("LocName")
Dim sqlStore As String = ""
sqlStore = "SELECT Store.StoreName FROM Location LEFT JOIN Store ON Location.StoreID = Store.StoreID WHERE Location.LocName = '" & Me.cbbLocation.Text & "'"
cmd = New OleDbCommand(sqlStore, Conn)
Dim adapter4 As New OleDbDataAdapter(cmd)
dataset4 = New DataSet()
adapter4.Fill(dataset4, "sqls")
cbbStore.Text = ""
cbbStore.Text = dataset4.Tables("sqls").Rows(0)("StoreName")
cbbStore.Enabled = False
End If
End If
End If
End If
End Sub
Private Sub Cleartxtpro()
txtnamepro.Text = ""
txtBrand.Text = ""
txtuom.Text = ""
txtcost.Text = ""
txtPrice.Text = ""
End Sub
Private Sub Clearcbb()
cbbLocation.Enabled = False
cbbLot.Enabled = False
cbbStore.Enabled = True
cbbLocation.Text = ""
cbbLot.Text = ""
End Sub
Private Sub btnSearchProducts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearchProducts.Click
frm_searchproduct.Show()
End Sub
Private Sub datapro()
txtnamepro.Text = dataset1.Tables("items").Rows(0)("ItemName")
txtBrand.Text = dataset1.Tables("items").Rows(0)("BrandName")
txtuom.Text = dataset1.Tables("items").Rows(0)("NameUnitOfMeasure")
txtcost.Text = dataset1.Tables("items").Rows(0)("UnitCost")
txtPrice.Text = dataset1.Tables("items").Rows(0)("UnitPrice")
txtqty.Focus()
End Sub
Private Sub cbbStore_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbStore.SelectedIndexChanged
cbbLocation.Enabled = True
Dim isFind1 As Boolean = False
Dim ds1 As New DataSet
Dim sql1 As String = "SELECT * FROM Location WHERE StoreID = '" & cbbStore.SelectedValue.ToString & "'"
If isFind1 = True Then
ds1.Tables("in").Clear()
End If
Dim da1 As New OleDbDataAdapter(sql1, Conn)
da1.Fill(ds1, "in")
If ds1.Tables("in").Rows.Count <> 0 Then
isFind1 = True
With cbbLocation
.DataSource = ds1.Tables("in")
.DisplayMember = "LocName"
.ValueMember = "LocID"
End With
Else
isFind1 = False
cbbLocation.DataSource = Nothing
End If
End Sub
Private Sub cbbLocation_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbLocation.SelectedIndexChanged
cbbLot.Enabled = True
Dim isFind1 As Boolean = False
Dim ds1 As New DataSet
Dim sql1 As String = "SELECT * FROM Lot WHERE LocID = '" & cbbLocation.SelectedValue.ToString & "' AND LotStatus = 'Y'"
If isFind1 = True Then
ds1.Tables("in").Clear()
End If
Dim da1 As New OleDbDataAdapter(sql1, Conn)
da1.Fill(ds1, "in")
If ds1.Tables("in").Rows.Count <> 0 Then
isFind1 = True
With cbbLot
.DataSource = ds1.Tables("in")
.DisplayMember = "LotNo"
.ValueMember = "LotNo"
End With
Else
isFind1 = False
cbbLot.DataSource = Nothing
End If
End Sub
Private Sub FormatdgvReceiveproduct()
dgvReceiveproduct.AlternatingRowsDefaultCellStyle.BackColor = Color.Bisque
With dgvReceiveproduct
.Columns.Add("rId", "รหัสรับ")
.Columns.Add("rItemCode", "รหัสสินค้า")
.Columns.Add("BrandName", "ยี่ห้อ")
.Columns.Add("rNameItem", "ชื่อสินค้า")
.Columns.Add("rQty", "จำนวน")
.Columns.Add("rMOU", "หน่วยนับ")
.Columns.Add("rCost", "ต้นทุน/หน่วย")
.Columns.Add("rTotalCost", "ต้นทุนรวม")
.Columns.Add("rLot", "หมายเลขล็อต")
.Columns.Add("rStore", "คลังสินค้า")
.Columns.Add("rLocation", "ที่จัดเก็บ")
.Columns.Add("rEnterDate", "วันที่บันทึก")
.Columns.Add("rEmployeeCode", "รหัสพนักงาน")
.Columns.Add("rEmployeeName", "ชื่อพนักงาน")
.Columns.Add("rUesrLogin", "ผู้บันทึก")
.Columns(1).Width = 80
.Columns(3).Width = 185
.Columns(4).Width = 50
.Columns(5).Width = 75
.Columns(6).Width = 95
.Columns(7).Width = 85
.Columns(8).Width = 95
.Columns(0).ReadOnly = True
.Columns(1).ReadOnly = True
.Columns(2).ReadOnly = True
.Columns(3).ReadOnly = True
.Columns(4).ReadOnly = True
.Columns(5).ReadOnly = True
.Columns(6).ReadOnly = True
.Columns(7).ReadOnly = True
.Columns(8).ReadOnly = True
.Columns(9).ReadOnly = True
.Columns(10).ReadOnly = True
.Columns(11).ReadOnly = True
.Columns(12).ReadOnly = True
.Columns(13).ReadOnly = True
.Columns(14).ReadOnly = True
.Columns(0).Visible = False
.Columns(2).Visible = False
.Columns(9).Visible = False
.Columns(10).Visible = False
.Columns(11).Visible = False
.Columns(12).Visible = False
.Columns(13).Visible = False
.Columns(14).Visible = False
End With
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
If (txtidr.Text = "") Then
MessageBox.Show("กรุณาใส่รหัสรับสินค้า", "ไม่ได้กรอกข้อมูล", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return
End If
If (txtid.Text = "") Then
MessageBox.Show("กรุณาใส่รหัสพนักงาน", "ไม่ได้กรอกข้อมูล", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return
End If
If (txtidpro.Text = "") Then
MessageBox.Show("กรุณาใส่รหัสสินค้า", "ไม่ได้กรอกข้อมูล", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return
End If
If (txtqty.Text = "") Then
MessageBox.Show("กรุณาใส่จำนวนสินค้า", "ไม่ได้กรอกข้อมูล", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return
End If
If (cbbLot.Text = "") Then
MessageBox.Show("กรุณาเลือกล็อตที่เก็บสินค้า", "ไม่ได้กรอกข้อมูล", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return
End If
With dgvReceiveproduct
For i As Integer = 0 To .Rows.Count - 1
tmpPid = .Rows(i).Cells("rLot").Value.ToString
If (tmpPid = cbbLot.Text.ToString) Then
MessageBox.Show("มีข้อมูลหมายเลขล็อตซ้ำ กรุณากรอกใหม่ด้วยครับ", "Duplicate", MessageBoxButtons.OK, MessageBoxIcon.Information)
cbbLot.Focus()
Return
End If
Next
End With
GetData()
End Sub
Sub GetData()
CurrentRow += 1
Dim Cost As Integer = 0
Dim Qty As Integer = 0
Dim TotalCost As Integer = 0
Cost = CInt(txtcost.Text)
Qty = CInt(txtqty.Text)
TotalCost = Cost * Qty
dgvReceiveproduct.Rows.Add(New String() {txtidr.Text, txtidpro.Text, txtBrand.Text, txtnamepro.Text, txtqty.Text, txtuom.Text, txtcost.Text, TotalCost.ToString.Trim, cbbLot.Text, cbbStore.Text, cbbLocation.Text, Dttd.ToString.Trim, txtid.Text, txtname.Text, frm_Home.tsslbl2.Text})
Cleartxtpro()
Clearcbb()
txtidpro.Text = ""
txtqty.Text = ""
txtidr.ReadOnly = True
txtid.ReadOnly = True
btnDelete.Enabled = True
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If CurrentRow <= -1 Then
btnDelete.Enabled = False
Return
End If
dgvReceiveproduct.Rows.RemoveAt(dgvReceiveproduct.CurrentRow.Index)
CurrentRow = dgvReceiveproduct.Rows.Count - 1
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim Ty As String = "R"
Dim i As Integer
If CurrentRow > 0 Then
For i = 0 To dgvReceiveproduct.RowCount - 1
Dim sql As String = ""
sql = "INSERT INTO TransactionStore(Tid, ItemCode, LotNo, Type, Qty, TotalCost, EnterDate, EmployeeCode, UserID)"
sql &= "VALUES(@idr, @ic, @ln ,@tp ,@qt ,@tc ,@ed ,@ec ,@uid)"
cmd = New OleDbCommand(sql, Conn)
With cmd
.CommandType = CommandType.Text
.CommandText = sql
.Connection = Conn
.Parameters.Clear()
.Parameters.AddWithValue("idr", txtidr.Text)
.Parameters.AddWithValue("ic", dgvReceiveproduct.Rows.Item(i).Cells(1).Value)
.Parameters.AddWithValue("ln", dgvReceiveproduct.Rows.Item(i).Cells(8).Value)
.Parameters.AddWithValue("tp", Ty.ToString.Trim)
.Parameters.AddWithValue("qt", dgvReceiveproduct.Rows.Item(i).Cells(4).Value)
.Parameters.AddWithValue("tc", dgvReceiveproduct.Rows.Item(i).Cells(7).Value)
.Parameters.AddWithValue("ed", dgvReceiveproduct.Rows.Item(i).Cells(11).Value)
.Parameters.AddWithValue("ec", dgvReceiveproduct.Rows.Item(i).Cells(12).Value)
.Parameters.AddWithValue("uid", frm_Home.tsslbl2.Text)
.ExecuteNonQuery()
.Parameters.Clear()
End With
Dim strSQL As String = ""
strSQL = "SELECT * FROM Stock WHERE ItemCode = '" & dgvReceiveproduct.Rows.Item(i).Cells(1).Value & "'"
cmd = New OleDbCommand(strSQL, Conn)
Dim adapter As New OleDbDataAdapter(cmd)
dataset1 = New DataSet()
adapter.Fill(dataset1, "Sic")
rowtxtidpro3 = dataset1.Tables("Sic").Rows.Count
Dim Onha As Integer = 0
Dim sqlStock As String = ""
If rowtxtidpro3 <= 0 Then
sqlStock = "INSERT INTO Stock (ItemCode, LotNo, Onhand)"
sqlStock &= "VALUES(@ice, @ltn, @ohd)"
cmd = New OleDbCommand(sqlStock, Conn)
With cmd
.CommandType = CommandType.Text
.CommandText = sqlStock
.Connection = Conn
.Parameters.Clear()
.Parameters.AddWithValue("ice", dgvReceiveproduct.Rows.Item(i).Cells(1).Value)
.Parameters.AddWithValue("ltn", dgvReceiveproduct.Rows.Item(i).Cells(8).Value)
.Parameters.AddWithValue("ohd", dgvReceiveproduct.Rows.Item(i).Cells(4).Value)
.ExecuteNonQuery()
.Parameters.Clear()
End With
Dim YN As String = "N"
Dim sqlYN As String = ""
sqlYN = "UPDATE Lot SET LotStatus = @lss WHERE LotNo = '" & dgvReceiveproduct.Rows.Item(i).Cells(8).Value & "'"
cmd = New OleDbCommand(sqlYN, Conn)
With cmd
.CommandType = CommandType.Text
.CommandText = sqlYN
.Connection = Conn
.Parameters.Clear()
.Parameters.AddWithValue("lss", YN.ToString.Trim)
.ExecuteNonQuery()
.Parameters.Clear()
End With
Else
Onha = CInt(dataset1.Tables("Sic").Rows(0)("Onhand")) + CInt(dgvReceiveproduct.Rows.Item(i).Cells(4).Value)
sqlStock = "UPDATE Stock SET ItemCode = @ice, LotNo = @ltn, Onhand = @ohd "
sqlStock &= "WHERE ItemCode = '" & dgvReceiveproduct.Rows.Item(i).Cells(1).Value & "'"
cmd = New OleDbCommand(sqlStock, Conn)
With cmd
.CommandType = CommandType.Text
.CommandText = sqlStock
.Connection = Conn
.Parameters.Clear()
.Parameters.AddWithValue("ice", dgvReceiveproduct.Rows.Item(i).Cells(1).Value)
.Parameters.AddWithValue("ltn", dgvReceiveproduct.Rows.Item(i).Cells(8).Value)
.Parameters.AddWithValue("ohd", Onha.ToString.Trim)
.ExecuteNonQuery()
.Parameters.Clear()
End With
End If
Next
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว", "ข้อความ", MessageBoxButtons.OK, MessageBoxIcon.None)
End If
RefreshAll()
txtid.ReadOnly = False
End Sub
Private Sub btnPrint1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint1.Click
End Sub
Private Sub btnPrint2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint2.Click
End Sub
Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
RefreshAll()
End Sub
Private Sub RefreshAll()
Cleartxtpro()
Clearcbb()
txtidr.Text = ""
txtid.Text = ""
txtname.Text = ""
txtidpro.Text = ""
txtqty.Text = ""
dgvReceiveproduct.Rows.Clear()
NewNB()
End Sub
Private Sub frm_receiveproduct_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
Conn.Close()
End Sub
End Class
Tag : .NET, Ms Access, Crystal Report, Report Others, Win (Windows App), VS 2005 (.NET 2.x)