Public Class Main
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
Dim ChildProduct_List As New Product_List
ChildProduct_List.MdiParent = Me
ChildProduct_List.Show()
End Sub
Private Sub FormChildClose()
For Each F As Form In Me.MdiChildren
F.Close()
Next
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
FormChildClose()
Dim ChildProduct_List As New Product_List
ChildProduct_List.MdiParent = Me
ChildProduct_List.Show()
End Sub
Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
FormChildClose()
Dim ChildSale As New Sale
ChildSale.MdiParent = Me
ChildSale.Show()
End Sub
Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
FormChildClose()
Dim ChildRPSale As New RPSale
ChildRPSale.MdiParent = Me
ChildRPSale.Show()
End Sub
Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
FormChildClose()
Dim ChildHelp As New Help
ChildHelp.MdiParent = Me
ChildHelp.Show()
End Sub
Private Sub ToolStripButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton6.Click
Dim ans As Byte
ans = MessageBox.Show("คุณต้องการออกจากโปรแกรมใช่หรือไม่", "System", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If ans = vbYes Then End
End Sub
Imports System.Data.OleDb
Imports System.IO
Public Class Product_List
Dim Conn As New OleDbConnection 'ตัวแปร Object OleDbConnection
Dim StrConn As String
Dim sqlcmd As String
Dim da As OleDbDataAdapter 'ตัวแปร Object OleDbDataAdapter
Dim ds As New DataSet
Dim actionFlag As String = ""
Private Sub Product_List_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
Me.ControlBox = False
sqlcmd = "Select CreamID,CreamName,Type,Price,Amount,Detail,Picture From CreamList Order by CreamID"
ShowData()
FormatAllDataGrid()
ClearForm(GroupBox1)
txtAmount.Enabled = False
lblCreamID.Enabled = False
txtCreamName.Enabled = False
txtDetail.Enabled = False
txtPrice.Enabled = False
cbbType_ID.Enabled = False
btnBrowse.Enabled = False
btnSave.Enabled = False
btnClear.Enabled = False
btnCancel.Enabled = False
btnAdd.Enabled = True
btnEdit.Enabled = False
btnDel.Enabled = False
DGVCream.Enabled = True
Dim sqlComboSystem1 As String
sqlComboSystem1 = "SELECT * FROM CreamType " 'เลือกจากตาราง
da = New OleDbDataAdapter(sqlComboSystem1, Conn) 'ให้ dataAdapter นิวคอเน็คโดยเรียกจาก เม็ดตอด Conn
da.Fill(ds, "System") 'ตั้งชื่อตารางเพื่อจำลองใน dataset
If ds.Tables("System").Rows.Count <> 0 Then
With cbbType_ID 'ชื่อ combobox
.BeginUpdate()
.DisplayMember = "TypeName" 'เลือกมาโชว์
.ValueMember = "TypeValue" 'จัดเรียงตาม id
.DataSource = ds.Tables("System") 'เลือกจาก dataset ชื่อ Rows
.EndUpdate()
End With
End If
End Sub
Private Sub FormatAllDataGrid()
Dim cs As New DataGridViewCellStyle()
cs.Font = New Font("MS sans serif", 10, FontStyle.Regular)
cs.Alignment = DataGridViewContentAlignment.MiddleCenter
With DGVCream
.ColumnHeadersDefaultCellStyle = cs
.Columns(0).HeaderText = "รหัสสินค้า"
.Columns(1).HeaderText = "ชื่อสินค้า"
.Columns(2).HeaderText = "ประเภทสินค้า"
.Columns(3).HeaderText = "ราคา"
.Columns(4).HeaderText = "จำนวน"
.Columns(5).HeaderText = "รายละเอียด"
.Columns(6).HeaderText = "รูป"
.Columns(0).Width = 80
.Columns(1).Width = 170
.Columns(2).Width = 100
.Columns(3).Width = 60
.Columns(4).Width = 60
.Columns(5).Width = 250
.Columns(6).Width = 70
End With
End Sub
Public Sub ShowData()
StrConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\SaleCream\SaleCream.mdb"
With Conn 'เริ่มต้นเชื่อมฐานข้อมูล
If .State = ConnectionState.Open Then .Close()
.ConnectionString = StrConn
.Open() 'เชื่อมต่อฐานข้อมูล
End With
If ds.Tables.Contains("CreamList") Then
ds.Tables("CreamList").Clear()
End If
da = New OleDbDataAdapter(sqlcmd, Conn)
da.Fill(ds, "CreamList") 'ตั้งชื่อชุดข้อมูล
With DGVCream
.ReadOnly = True
.DataSource = ds.Tables("CreamList") 'แสดงข้อมูลในคอนโทรล DataGridView
End With
End Sub
Private Sub ClearForm(ByRef oGroupBx As GroupBox)
Dim i, intTemp As Integer
intTemp = oGroupBx.Controls.Count
For i = 0 To intTemp - 1
With oGroupBx.Controls
If .Item(i).GetType() Is GetType(TextBox) Then
With CType(.Item(i), TextBox)
.Clear()
End With
End If
If .Item(i).GetType() Is GetType(MaskedTextBox) Then
With CType(.Item(i), MaskedTextBox)
.Clear()
End With
End If
If .Item(i).GetType() Is GetType(ComboBox) Then
With CType(.Item(i), ComboBox)
.SelectedIndex = -1
End With
End If
End With
Next
End Sub
Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
Dim txtSearch As String
txtSearch = Trim(txtFind.Text)
If txtSearch = "" Then
sqlcmd = "Select * From CreamList Order by CreamID"
MessageBox.Show("คุณยังไม่ได้กรอกรหัสหรือชื่อผลิตภัณฑ์ !!!", "ข้อความจากระบบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
sqlcmd = "Select * From CreamList where CreamID like '%" & txtSearch & "%' or CreamName like '%" & txtSearch & "%' Order by CreamID"
End If
ShowData()
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
ClearForm(GroupBox1)
lblCreamID.Text = ""
lblCID.Text = ""
cbbType_ID.Text = ""
PictureBox.Image = Nothing
PictureBox.Refresh()
PictureBox.ImageLocation = ""
txtAmount.Enabled = True
txtCreamName.Enabled = True
txtDetail.Enabled = True
txtPrice.Enabled = True
cbbType_ID.Enabled = True
btnBrowse.Enabled = True
btnSave.Enabled = True
btnClear.Enabled = True
btnCancel.Enabled = True
btnAdd.Enabled = False
btnEdit.Enabled = False
btnDel.Enabled = False
DGVCream.Enabled = False
txtCreamName.Focus()
actionFlag = "ADD"
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
lblCreamID.Text = Convert.ToString(DGVCream.CurrentRow.Cells(0).Value)
txtCreamName.Text = Convert.ToString(DGVCream.CurrentRow.Cells(1).Value)
cbbType_ID.Text = Convert.ToString(DGVCream.CurrentRow.Cells(2).Value)
txtPrice.Text = Convert.ToString(DGVCream.CurrentRow.Cells(3).Value)
txtAmount.Text = Convert.ToString(DGVCream.CurrentRow.Cells(4).Value)
txtDetail.Text = Convert.ToString(DGVCream.CurrentRow.Cells(5).Value)
PictureBox.ImageLocation = "D:\SaleCream\Image\" + Convert.ToString(DGVCream.CurrentRow.Cells(6).Value)
txtAmount.Enabled = True
txtCreamName.Enabled = True
txtDetail.Enabled = True
txtPrice.Enabled = True
cbbType_ID.Enabled = True
btnBrowse.Enabled = True
btnSave.Enabled = True
btnClear.Enabled = True
btnCancel.Enabled = True
btnAdd.Enabled = False
btnEdit.Enabled = False
btnDel.Enabled = False
DGVCream.Enabled = False
txtCreamName.Focus()
actionFlag = "EDIT"
End Sub
Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
Dim cTxtDel As String = Trim(Convert.ToString(DGVCream.CurrentRow.Cells(0).Value))
If cTxtDel = "" Then
MessageBox.Show("กรุณากลับไปเลือกข้อมูลที่จะลบก่อน")
Return
End If
If MsgBoxResult.Yes = MessageBox.Show("คุณแน่ใจที่จะลบข้อมูลใช่หรือไม่ ?", "ยืนยัน", MessageBoxButtons.YesNo) Then
sqlcmd = "Delete From CreamList where CreamID = '" & cTxtDel & "'"
Dim cn As New System.Data.OleDb.OleDbConnection(StrConn)
Dim cm As New OleDbCommand(sqlcmd, cn)
Dim dr As OleDbDataReader
cn.Open()
dr = cm.ExecuteReader
cn.Close()
If ds.Tables.Contains("CreamType") Then
ds.Tables("CreamType").Clear()
End If
sqlcmd = "Select * From CreamList Order by CreamID" 'เลือกข้อมูลจากตารางสินค้าโดยไม่มีเงื่อนไข
ShowData()
End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
OpenFileDialog1.Filter = "ไฟล์รูปภาพ(*.jpg,*.bmp,*.gif)|*.jpg;*.bmp;*.gif" ' ให้เลือกรูปภาพเฉพาะ Jpg Bmp Gif เท่านั้น
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
PictureBox.ImageLocation = OpenFileDialog1.FileName
End If
End Sub
Private Sub Autonumber()
Dim sqlTmp As String = "" 'ตัวแปรเก็บชุดคำสั่ง sql
Dim comTmp As OleDbCommand = New OleDbCommand 'ตัวแปรออบเจ็กต์ OleDbCommmand ที่ชื่อว่า comTmp
Dim drTmp As OleDbDataReader 'ตัวแปรออบเจ็กต์ OleDbDataReader ที่ชื่อว่า drTmp
Dim tmpMember_id As Integer = 0 'ตัวแปรเก็บรหัสสมาชิกที่ได้
Dim aType As String = cbbType_ID.Text
sqlTmp = "Select MAX(CID) AS CID From CreamList Where Type='" & aType & "'" 'สร้างชุดคำสั่ง SQl เพื่อเลือกข้อมูล 1 เร็คคอร์ดล่าสุด จากตาราง "SELECT TOP 1 EmpID FROM tbEMP ORDER BY EmpID DESC"
Try 'ให้ทำ
With comTmp
.CommandType = CommandType.Text
.CommandText = sqlTmp 'ใช้ชุดคำสั่ง sql ที่เก็บอยู่ในตัวแปร sqlTmp
.Connection = Conn 'ใช้การเชื่อมต่อของออบเจ็กต์ Connection
drTmp = .ExecuteReader 'รันชุดคำสั่ง SQl เก็บผลการทำงานไว้ที่ drTmp
drTmp.Read() 'อ่านข้อมูลในออบเจ็กต์ drTmp
'อ่านข้อมูลจากฟิลต์ Member_id เก็บไว้ในตัวแปร tmpMember_id
tmpMember_id = CInt(drTmp.Item("CID"))
drTmp.Close() 'ปิดออบเจ็กต์ drTmp
tmpMember_id = tmpMember_id + 1 'เพิ่มค่าอีก 1
'แสดงรหัสสมาชิกการที่สร้างได้
lblCID.Text = tmpMember_id.ToString("000")
End With
Catch ex As Exception 'ในกรณีที่ไม่มีจำนวนเร็คคอร์ดอยู่เลย
lblCID.Text = "001" 'กำหนดรหัสสมาชิกเท่ากับ 0001
End Try
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If txtCreamName.Text = "" Then
MessageBox.Show("คุณยังไม่ได้ใส่ชื่อสินค้า !", "ไม่สามารถบันทึกได้", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtCreamName.Focus()
Return
ElseIf cbbType_ID.Text = "" Then
MessageBox.Show("คุณยังไม่ได้ใส่ประเภทสินค้า !", "ไม่สามารถบันทึกได้", MessageBoxButtons.OK, MessageBoxIcon.Warning)
cbbType_ID.Focus()
Return
ElseIf txtDetail.Text = "" Then
MessageBox.Show("คุณยังไม่ได้ใส่รายละเอียด !", "ไม่สามารถบันทึกได้", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtDetail.Focus()
Return
ElseIf txtPrice.Text = "" Then
MessageBox.Show("คุณยังไม่ได้ใส่ราคาสินค้า !", "ไม่สามารถบันทึกได้", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtPrice.Focus()
Return
ElseIf txtAmount.Text = "" Then
MessageBox.Show("คุณยังไม่ได้ใส่จำนวนสินค้า !", "ไม่สามารถบันทึกได้", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtAmount.Focus()
Return
End If
If PictureBox.ImageLocation = "" Then
MessageBox.Show("คุณยังไม่ได้ใส่รูป !", "ไม่สามารถบันทึกได้", MessageBoxButtons.OK, MessageBoxIcon.Warning)
btnBrowse.Focus()
Return
End If
If MessageBox.Show("คุณต้องการบันทึกข้อมูล ใช่ หรือ ไม่", "ผลการบันทึกข้อมูล", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) = Windows.Forms.DialogResult.OK Then
Dim sqlcmd As String
Dim ename As String = txtCreamName.Text.Trim
Dim etype As String = cbbType_ID.Text.Trim
Dim eprice As String = txtPrice.Text.Trim
Dim eamount As String = txtAmount.Text.Trim
Dim edetail As String = txtDetail.Text.Trim
Dim cPicpath = PictureBox.ImageLocation
Dim cFileName = Path.GetFileName(cPicpath)
Dim cDestination As String
Select Case actionFlag
Case "ADD"
Autonumber()
Dim vType As String = cbbType_ID.SelectedValue
Dim CID As String = lblCID.Text
lblCreamID.Text = vType + CID
Dim eid As String = lblCreamID.Text.Trim
sqlcmd = "INSERT INTO CreamList (CreamID,CreamName,Type,Price,Amount,Detail,Picture,CID) VALUES ('" + eid + "','" + ename + "','" & etype & "','" + eprice + "','" & eamount & "','" & edetail & "','" & cFileName & "','" & CID & "')"
Case "EDIT"
Dim eid As String = lblCreamID.Text.Trim
sqlcmd = "Update CreamList set CreamName='" + ename + "',Type='" + etype + "',Price='" + eprice + "',Amount='" + eamount + "',Detail='" + edetail + "',Picture='" + cFileName + "' Where CreamID = '" & eid & "'"
End Select
Dim cn As New System.Data.OleDb.OleDbConnection(StrConn)
Dim cm As New OleDbCommand(sqlcmd, cn)
Dim dr As OleDbDataReader
cn.Open()
dr = cm.ExecuteReader
cn.Close()
cDestination = "D:\SaleCream\Image\" + cFileName
If Not String.IsNullOrEmpty(cPicpath) And (cPicpath <> cDestination) Then
FileSystem.FileCopy(cPicpath, cDestination)
End If
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว")
sqlcmd = "Select CreamID,CreamName,Type,Price,Amount,Detail,Picture From CreamList Order by CreamID" 'เลือกข้อมูลจากตารางสินค้าโดยไม่มีเงื่อนไข
ShowData()
ClearForm(GroupBox1)
PictureBox.Image = Nothing
PictureBox.Refresh()
PictureBox.ImageLocation = ""
txtAmount.Enabled = False
lblCreamID.Enabled = False
txtCreamName.Enabled = False
txtDetail.Enabled = False
txtPrice.Enabled = False
cbbType_ID.Enabled = False
btnBrowse.Enabled = False
btnSave.Enabled = False
btnClear.Enabled = False
btnCancel.Enabled = False
btnAdd.Enabled = True
btnEdit.Enabled = False
btnDel.Enabled = False
DGVCream.Enabled = True
End If
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
ClearForm(GroupBox1)
cbbType_ID.Text = ""
PictureBox.Image = Nothing
PictureBox.Refresh()
PictureBox.ImageLocation = ""
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
ClearForm(GroupBox1)
PictureBox.Image = Nothing
PictureBox.Refresh()
PictureBox.ImageLocation = ""
txtAmount.Enabled = False
lblCreamID.Enabled = False
txtCreamName.Enabled = False
txtDetail.Enabled = False
txtPrice.Enabled = False
cbbType_ID.Enabled = False
btnBrowse.Enabled = False
btnSave.Enabled = False
btnClear.Enabled = False
btnCancel.Enabled = False
btnAdd.Enabled = True
btnEdit.Enabled = False
btnDel.Enabled = False
DGVCream.Enabled = True
End Sub
Private Sub DGVCream_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DGVCream.MouseUp
lblCreamID.Text = Convert.ToString(DGVCream.CurrentRow.Cells(0).Value)
txtCreamName.Text = Convert.ToString(DGVCream.CurrentRow.Cells(1).Value)
cbbType_ID.Text = Convert.ToString(DGVCream.CurrentRow.Cells(2).Value)
txtPrice.Text = Convert.ToString(DGVCream.CurrentRow.Cells(3).Value)
txtAmount.Text = Convert.ToString(DGVCream.CurrentRow.Cells(4).Value)
txtDetail.Text = Convert.ToString(DGVCream.CurrentRow.Cells(5).Value)
PictureBox.ImageLocation = "D:\SaleCream\Image\" + Convert.ToString(DGVCream.CurrentRow.Cells(6).Value)
btnAdd.Enabled = True
btnEdit.Enabled = True
btnDel.Enabled = True
End Sub
End Class
Imports System.Data.OleDb
Imports System.IO
Public Class Product_Type
Dim Conn As New OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As New DataSet
Dim strconn As String
Dim Isfind As Boolean = False
Dim Isfind2 As Boolean = False
Dim sqlcmd As String
Private Sub Product_Type_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
Me.ControlBox = False
strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\SaleCream\SaleCream.mdb"
With Conn 'เริ่มการเชื่อมต่อ
If .State = ConnectionState.Open Then Close()
.ConnectionString = strconn
.Open()
End With
showData()
If ds.Tables("CreamType").Rows.Count <> 0 Then
FormatAllDataGrid()
End If
ClearForm()
txtType.Enabled = False
txtTypeValue.Enabled = False
btnClear.Enabled = False
btnSave.Enabled = False
btnDel.Enabled = False
btnAdd.Enabled = True
dgvCreamType.Enabled = True
End Sub
Private Sub ClearForm()
lblCID.Text = ""
txtType.Text = ""
txtTypeValue.Text = ""
End Sub
Private Sub showData()
sqlcmd = "SELECT * FROM CreamType"
If ds.Tables.Contains("CreamType") Then
ds.Tables("CreamType").Clear()
End If
da = New OleDbDataAdapter(sqlcmd, Conn)
da.Fill(ds, "CreamType") 'ตั้งชื่อชุดข้อมูล
With dgvCreamType
.ReadOnly = True
.DataSource = ds.Tables("CreamType") 'แสดงข้อมูลในคอนโทรล DataGridView
End With
End Sub
Private Sub FormatAllDataGrid()
Dim cs As New DataGridViewCellStyle()
cs.Font = New Font("MS sans serif", 10, FontStyle.Regular)
cs.Alignment = DataGridViewContentAlignment.MiddleCenter
With dgvCreamType
.ColumnHeadersDefaultCellStyle = cs
.Columns(0).HeaderText = "ลำดับที่"
.Columns(1).HeaderText = "ประเภทสินค้า"
.Columns(2).HeaderText = "ตัวย่อ"
.Columns(0).Width = 80
.Columns(1).Width = 250
.Columns(2).Width = 80
End With
End Sub
Private Sub Autonumber()
Try
Dim sqlAuto As String
Dim comAuto As New OleDbCommand
sqlAuto = "SELECT MAX(TypeID)+1 AS TypeID"
sqlAuto &= " FROM CreamType"
Dim da As New OleDbDataAdapter(sqlAuto, Conn)
da.Fill(ds, "CreamType1")
lblCID.Text = CStr(ds.Tables("CreamType1").Rows(0).Item("TypeID"))
Catch ex As Exception 'ในกรณีที่ไม่มีจำนวนเร็คคอร์ดอยู่เลย
lblCID.Text = "1" 'กำหนดรหัสสมาชิกเท่ากับ 0001
End Try
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Autonumber()
txtType.Enabled = True
txtTypeValue.Enabled = True
btnClear.Enabled = True
btnSave.Enabled = True
btnDel.Enabled = False
btnAdd.Enabled = False
dgvCreamType.Enabled = False
txtType.Focus()
End Sub
Private Sub dgvCreamType_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvCreamType.MouseUp
btnAdd.Enabled = True
btnDel.Enabled = True
End Sub
Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
Dim cTxtDel As String = Trim(Convert.ToString(dgvCreamType.CurrentRow.Cells(0).Value))
If cTxtDel = "" Then
MessageBox.Show("กรุณากลับไปเลือกข้อมูลที่จะลบก่อน")
Return
End If
Dim comDelete As New OleDbCommand
If MsgBoxResult.Yes = MessageBox.Show("คุณแน่ใจที่จะลบข้อมูลใช่หรือไม่ ?", "ยืนยัน", MessageBoxButtons.YesNo) Then
sqlcmd = "Delete From CreamType where TypeID = " & cTxtDel & ""
With comDelete
.CommandType = CommandType.Text
.CommandText = sqlcmd
.Connection = Conn
.ExecuteNonQuery()
End With
If ds.Tables.Contains("CreamType") Then
ds.Tables("CreamType").Clear()
End If
showData()
End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim sqlAdd As String
Dim sqlselect As String
Dim comAdd As New OleDbCommand
If txtType.Text = "" Then
MessageBox.Show("คุณยังไม่ได้ใส่ประเภทสินค้า !", "ไม่สามารถบันทึกได้", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtType.Focus()
Return
ElseIf txtTypeValue.Text = "" Then
MessageBox.Show("คุณยังไม่ได้ใส่ตัวย่อประเภทสินค้า !", "ไม่สามารถบันทึกได้", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtTypeValue.Focus()
Return
End If
If Isfind2 = True Then
ds.Tables("SaveCount").Clear()
End If
sqlselect = "select * from CreamType Where TypeName = '" & txtType.Text & "' "
da = New OleDbDataAdapter(sqlselect, Conn) ' ประมวลผล SQL เก็บข้อมูลพนักงานที่ได้ไว้ใน DataAdapter
da.Fill(ds, "SaveCount")
If ds.Tables("SaveCount").Rows.Count <> 0 Then
MessageBox.Show("ไม่สามารถเพิ่มข้อมูลได้ เนื่องจากมีข้อมูลนี้อยู่ในระบบแล้ว", "ไม่สามารถเพิ่มได้")
Isfind2 = True
Else
sqlAdd = "Insert into CreamType(TypeID,TypeName,TypeValue)"
sqlAdd &= "Values ('" & lblCID.Text.Trim & "',"
sqlAdd &= "'" & txtType.Text.Trim & "',"
sqlAdd &= "'" & txtTypeValue.Text.Trim & "')"
With comAdd
.CommandType = CommandType.Text
.CommandText = sqlAdd
.Connection = Conn
.ExecuteNonQuery()
End With
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว", "ข้อความจากระบบ")
showData()
ClearForm()
txtType.Enabled = False
txtTypeValue.Enabled = False
btnClear.Enabled = False
btnSave.Enabled = False
btnDel.Enabled = False
btnAdd.Enabled = True
dgvCreamType.Enabled = True
ds.Tables("CreamType1").Clear()
Isfind2 = True
End If
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
ClearForm()
txtType.Enabled = False
txtTypeValue.Enabled = False
btnClear.Enabled = False
btnSave.Enabled = False
btnDel.Enabled = False
btnAdd.Enabled = True
dgvCreamType.Enabled = True
End Sub
End Class
หน้ารายงาน (ออกเป็น Crystal Report) Code (VB.NET)
Imports System.Data.OleDb
Imports System.IO
Public Class RPProduct
Dim Conn As New OleDbConnection 'ตัวแปร Object OleDbConnection
Dim StrConn As String
Dim sqlcmd As String
Dim da As OleDbDataAdapter 'ตัวแปร Object OleDbDataAdapter
Dim ds As New DataSet
Private Sub RPProduct_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
Me.ControlBox = False
sqlcmd = "Select CreamID,CreamName,Type,Price,Amount,Detail From CreamList Order by CreamID"
ShowData()
If ds.Tables("CreamList").Rows.Count <> 0 Then
FormatAllDataGrid()
lblAmount.Text = DGVCream.Rows.Count - 1
Else
lblAmount.Text = DGVCream.Rows.Count
End If
End Sub
Private Sub FormatAllDataGrid()
Dim cs As New DataGridViewCellStyle()
cs.Font = New Font("MS sans serif", 10, FontStyle.Regular)
cs.Alignment = DataGridViewContentAlignment.MiddleCenter
With DGVCream
.ColumnHeadersDefaultCellStyle = cs
.Columns(0).HeaderText = "รหัสสินค้า"
.Columns(1).HeaderText = "ชื่อสินค้า"
.Columns(2).HeaderText = "ประเภทสินค้า"
.Columns(3).HeaderText = "ราคา"
.Columns(4).HeaderText = "จำนวน"
.Columns(5).HeaderText = "รายละเอียด"
.Columns(0).Width = 80
.Columns(1).Width = 220
.Columns(2).Width = 120
.Columns(3).Width = 60
.Columns(4).Width = 60
.Columns(5).Width = 370
End With
End Sub
Public Sub ShowData()
StrConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\SaleCream\SaleCream.mdb"
With Conn 'เริ่มต้นเชื่อมฐานข้อมูล
If .State = ConnectionState.Open Then .Close()
.ConnectionString = StrConn
.Open() 'เชื่อมต่อฐานข้อมูล
End With
'sqlcmd = "Select * From Book Order by ISBN"
If ds.Tables.Contains("CreamList") Then
ds.Tables("CreamList").Clear()
End If
da = New OleDbDataAdapter(sqlcmd, Conn)
da.Fill(ds, "CreamList") 'ตั้งชื่อชุดข้อมูล
With DGVCream
.ReadOnly = True
.DataSource = ds.Tables("CreamList") 'แสดงข้อมูลในคอนโทรล DataGridView
End With
End Sub
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim txtSearch As String
txtSearch = Trim(txtFind.Text)
If txtSearch = "" Then
sqlcmd = "Select * From CreamList Order by CreamID"
MessageBox.Show("คุณยังไม่ได้กรอกรหัสหรือชื่อผลิตภัณฑ์ !!!", "ข้อความจากระบบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
sqlcmd = "Select * From CreamList where CreamID like '%" & txtSearch & "%' or CreamName like '%" & txtSearch & "%' Order by CreamID"
End If
ShowData()
End Sub
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Try
'เคลียร์ค่าใน dtEx ออก
If SaleCreamDataSet1.CreamList.Rows.Count > 0 Then
SaleCreamDataSet1.CreamList.Rows.Clear()
End If
'วน loop เพื่อเก็บค่าจาก DatagridView ใส่ใน DataTable dtEx
For i As Integer = 0 To DGVCream.Rows.Count - 2
Dim dr As DataGridViewRow
dr = DGVCream.Rows(i)
SaleCreamDataSet1.CreamList.Rows.Add(dr.Cells(0).Value, dr.Cells(1).Value, dr.Cells(2).Value, dr.Cells(3).Value, dr.Cells(4).Value, dr.Cells(5).Value)
'ต้องตรงตามจำนวน column
Next
'แสดงรายงาน
Dim crv As New CrystalReport1
crv.SetDataSource(SaleCreamDataSet1)
Dim frm As New Fr_RPProduct
frm.CrystalReportViewer1.ReportSource = crv
frm.MdiParent = Main
frm.Show()
Me.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class