Imports System
Imports System.Data ' ระบุ Namespace สำหรับการใช้งานฐานข้อมูล
Imports System.Data.OleDb ' ระบุ Namespace สำหรับการใช้งาน Oledb
Imports System.Data.Common
Imports System.Data.DataTable
Imports System.IO
Public Class BK_ROOM_R
Dim OleDbConnection1 As New OleDbConnection ' ประกาศตัวแปรสำหรับจัดเก็บการเชื่อมต่อฐานข้อมูล
Dim da As OleDbDataAdapter = Nothing ' ประกาศตัวแปรสำหรับจัดเก็บ DataAdapter
Dim ds As New DataSet ' ประกาศตัวแปรสำหรับจัดเก็บ DataSet
Dim dt As DataTable = Nothing ' ประกาศตัวแปรสำหรับจัดเก็บ DataTable
Dim da1 As OleDbDataAdapter = Nothing ' ประกาศตัวแปรสำหรับจัดเก็บ DataAdapter
Dim ds1 As New DataSet ' ประกาศตัวแปรสำหรับจัดเก็บ DataSet
Dim dt1 As DataTable = Nothing ' ประกาศตัวแปรสำหรับจัดเก็บ DataTable
Dim tempshowrecord As Integer
Dim reccnt As Integer
Dim DDR As DbDataReader
Dim DTB As DataTable
Dim cnt As Integer
Dim recttl As Integer
Dim TllLvlRoom As Integer
Dim CntLvlRoom As Integer
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim cmd As OleDbCommand = Nothing
Dim sqlSave As String = ""
Dim sqlUpdate As String = ""
Dim j As Integer
Dim sqlif As String = "SELECT BRR_ROOM_CODE FROM BK_ROOM_R WHERE BRR_ROOM_CODE LIKE '" & Txt_Id.Text & "' "
da1 = New OleDbDataAdapter(sqlif, OBCon)
da1.Fill(ds1, "dsif")
dt1 = ds1.Tables("dsif")
j = Val(dt1.Rows.Count)
ds1.Tables("dsif").Clear()
dt1 = ds1.Tables("dsif")
If Txt_Id.Text = "" Then
MessageBox.Show("กรุณากรอกข้อมูล", "คำเตือน", MessageBoxButtons.OK, MessageBoxIcon.Information)
Txt_Id.Focus()
Exit Sub
ElseIf Cbo1.Text = "" Then
MessageBox.Show("กรุณากรอกข้อมูล", "คำเตือน", MessageBoxButtons.OK, MessageBoxIcon.Information)
Cbo1.Focus()
Exit Sub
ElseIf Cbo2.Text = "" Then
MessageBox.Show("กรุณากรอกข้อมูล", "คำเตือน", MessageBoxButtons.OK, MessageBoxIcon.Information)
Cbo2.Focus()
Exit Sub
ElseIf MessageBox.Show("ทำการบันทึก " & Txt_Id.Text & " ", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
Exit Sub
End If
Try
Catch ex As Exception
End Try
Try
If j = 0 Then
'R_Number()
LoadTllLvlRoom()
LoadTCntLvlRoom()
If (CntLvlRoom >= TllLvlRoom) Then
MessageBox.Show("จำนวนห้องที่กำหนดครบแล้ว ไม่สามารถเพ่ิ่มได้อีก!!!", "คำเตือน", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
' sqlSave = "INSERT INTO BK_ROOM_R(BRR_ROOM_CODE,BRR_ROOM_LEVEL,BRR_TYPE_ID,BRR_STATUS) VALUES( '" & Txt_Id.Text & "','" & Cbo1.SelectedValue & "','" & Cbo2.SelectedValue & "','" & Cbo3.SelectedValue & "')"
sqlSave = "INSERT INTO BK_ROOM_R(BRR_ROOM_CODE,BRR_ROOM_LEVEL,BRR_TYPE_ID) VALUES( '" & Txt_Id.Text & "','" & Cbo1.SelectedValue & "','" & Cbo2.SelectedValue & "')"
tempshowrecord = Val(Txt_Id.Text.ToString)
cmd = New OleDbCommand(sqlSave, OBCon)
cmd.ExecuteNonQuery()
End If
Else
sqlUpdate = "UPDATE BK_ROOM_R SET BRR_ROOM_CODE='" & Txt_Id.Text & "',BRR_ROOM_LEVEL= '" & Cbo1.SelectedValue & "',BRR_TYPE_ID= '" & Cbo2.SelectedValue & "' WHERE BRR_ROOM_CODE = '" & Txt_Id.Text & "'"
tempshowrecord = Val(Txt_Id.Text.ToString)
cmd = New OleDbCommand(sqlUpdate, OBCon)
cmd.ExecuteNonQuery()
End If
Catch ex As Exception ' กรณีประมวลผลในช่วงของ Try แล้วมีข้อผิดพลาด จะมีการกระโดด(jump) ลงมาที่บรรทัดนี้เลย เพื่อแสดงข้อผิดพลาดของโปรแกรม
MessageBox.Show(ex.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Finally
'cleargrid()
Showrecord()
End Try
End Sub
Private Sub LoadTllLvlRoom()
Dim strsql As String
strsql = "select blr_numroom, blr_room_level from bk_level_r where blr_room_level = '" & Cbo1.SelectedValue.ToString & "' "
OBCom = New OleDbCommand()
Dim DDR As DbDataReader
Dim DTB As DataTable
With OBCom
.CommandText = strsql
.CommandType = CommandType.Text
.Connection = OBCon
DDR = .ExecuteReader()
End With
If DDR.HasRows Then
DTB = New DataTable()
DTB.Load(DDR)
TllLvlRoom = DTB.Rows(0).Item("blr_numroom")
Else
TllLvlRoom = 0
End If
End Sub
Private Sub LoadTCntLvlRoom()
Dim strsql As String
strsql = "SELECT count(*) as cntrec FROM BK_ROOM_R where brr_room_level = '" & Cbo1.SelectedValue.ToString & "' "
OBCom = New OleDbCommand()
Dim DDR As DbDataReader
Dim DTB As DataTable
With OBCom
.CommandText = strsql
.CommandType = CommandType.Text
.Connection = OBCon
DDR = .ExecuteReader()
End With
If DDR.HasRows Then
DTB = New DataTable()
DTB.Load(DDR)
CntLvlRoom = DTB.Rows(0).Item("cntrec")
Else
CntLvlRoom = 0
End If
End Sub
Private Sub ROOMCbo1()
Dim SqlTxt As String
SqlTxt = "SELECT BLR_ROOM_LEVEL FROM BK_LEVEL_R "
OBCom = New OleDbCommand()
Dim DDR As DbDataReader
Dim DTB As DataTable
With OBCom
.CommandText = SqlTxt
.CommandType = CommandType.Text
.Connection = OBCon
DDR = .ExecuteReader()
End With
If DDR.HasRows Then
DTB = New DataTable()
DTB.Load(DDR)
With Cbo1
.BeginUpdate()
.DisplayMember = "BLR_ROOM_LEVEL"
.ValueMember = "BLR_ROOM_LEVEL"
.DataSource = DTB
.EndUpdate()
End With
End If
Cbo1.Enabled = True
End Sub
Private Sub ROOMCbo2()
Dim SqlTxt As String
SqlTxt = "SELECT BTRR_TYPE_NAME,BTRR_TYPE_ID FROM BK_TYPE_ROOM_R "
OBCom = New OleDbCommand()
Dim DDR As DbDataReader
Dim DTB As DataTable
With OBCom
.CommandText = SqlTxt
.CommandType = CommandType.Text
.Connection = OBCon
DDR = .ExecuteReader()
End With
If DDR.HasRows Then
DTB = New DataTable()
DTB.Load(DDR)
With Cbo2
.BeginUpdate()
.DisplayMember = "BTRR_TYPE_NAME"
.ValueMember = "BTRR_TYPE_ID"
.DataSource = DTB
.EndUpdate()
End With
End If
Cbo2.Enabled = True
End Sub
Private Sub ROOMCbo3()
Dim SqlTxt As String
SqlTxt = "SELECT BSRR_STATUS,BSRR_STATUS_ID FROM BK_STATUS_ROOM_R "
OBCom = New OleDbCommand()
Dim DDR As DbDataReader
Dim DTB As DataTable
With OBCom
.CommandText = SqlTxt
.CommandType = CommandType.Text
.Connection = OBCon
DDR = .ExecuteReader()
End With
If DDR.HasRows Then
DTB = New DataTable()
DTB.Load(DDR)
With Cbo3
.BeginUpdate()
.DisplayMember = "BSRR_STATUS"
.ValueMember = "BSRR_STATUS_ID"
.DataSource = DTB
.EndUpdate()
End With
End If
Cbo3.Enabled = True
End Sub
Private Sub Showrecord()
'LoadTllLvlRoom()
Dim sqlselect As String
'sqlselect = " select bk_room_r.brr_room_code,BK_ROOM_R.brr_room_level,bk_type_room_r.btrr_type_name,BK_STATUS_ROOM_R.bsrr_status from BK_ROOM_R,bk_type_room_r,BK_STATUS_ROOM_R where BK_ROOM_R.brr_type_id = bk_type_room_r.btrr_type_id and bk_room_r.brr_status = bk_status_room_r.bsrr_status_id order by bk_room_r.brr_room_code ASC"
sqlselect = " select bk_room_r.brr_room_code,BK_ROOM_R.brr_room_level,bk_type_room_r.btrr_type_name from BK_ROOM_R,bk_type_room_r where bk_room_r.brr_type_id = bk_type_room_r.btrr_type_id order by bk_room_r.brr_room_code ASC"
OBCom = New OleDbCommand()
Dim DDR As DbDataReader
Dim DTB As DataTable
With OBCom
.CommandText = sqlselect
.CommandType = CommandType.Text
.Connection = OBCon
DDR = .ExecuteReader()
End With
If DDR.HasRows Then
DTB = New DataTable()
DTB.Load(DDR)
dgv1.ReadOnly = True
dgv1.DataSource = DTB
dgv1.Columns(0).HeaderText = "เลขที่ห้อง"
dgv1.Columns(0).Width = 150
dgv1.Columns(0).DataPropertyName = "BRR_ROOM_CODE"
dgv1.Columns(1).HeaderText = "ลำดับชั้น"
dgv1.Columns(1).Width = 200
dgv1.Columns(1).DataPropertyName = "BRR_ROOM_LEVEL"
dgv1.Columns(2).HeaderText = "ประเภทห้อง"
dgv1.Columns(2).Width = 300
dgv1.Columns(2).DataPropertyName = "BTRR_TYPE_NAME"
'dgv1.Columns(3).HeaderText = "ชื่อสถานะห้อง"
'dgv1.Columns(3).Width = 300
'dgv1.Columns(3).DataPropertyName = "brr_status"
Else
dgv1.DataSource = Nothing
dgv1.Refresh()
End If
End Sub
Private Sub R_Number()
Dim sql_ As String
sql_ = " SELECT MAX(BRR_ROOM_CODE)+1 AS id FROM BK_ROOM_R ORDER BY BRR_ROOM_CODE ASC "
Dim comStr As New OleDbCommand(sql_, OBCon)
Dim crm As OleDbDataReader = comStr.ExecuteReader
If crm.Read Then
If IsDBNull(crm.Item("id")) = True Then ' ถ้า CustomerID ในดาต้าเบส ว่าง
Txt_Id.Text = ""
ElseIf IsDBNull(crm.Item("id")) = False Then ' ถ้า CostomerID ไม่ว่าง
Dim Last_CostomerID As String
Last_CostomerID = (crm.Item("id"))
Dim Next_CostomerID As Integer
Next_CostomerID = (Last_CostomerID.Substring(0)) + 1 - 1
Txt_Id.Text = Next_CostomerID.ToString("00")
End If
End If
End Sub
Private Sub Dgv1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgv1.SelectionChanged
reccnt = dgv1.CurrentRow.Index
Txt_Id.Text = dgv1.Rows(reccnt).Cells(0).Value.ToString()
Cbo1.Text = dgv1.Rows(reccnt).Cells(1).Value.ToString()
Cbo2.Text = dgv1.Rows(reccnt).Cells(2).Value.ToString()
'Cbo3.Text = dgv1.Rows(reccnt).Cells(3).Value.ToString()
End Sub
Private Sub Dgv1_ColumnAdded(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewColumnEventArgs) Handles dgv1.ColumnAdded
dgv1.Columns.Item(e.Column.Index).SortMode = DataGridViewColumnSortMode.NotSortable ' sort ข้อมูลไม่ได้
End Sub
Private Sub BK_DEPARTMENT_R_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Connect()
ROOMCbo1()
ROOMCbo2()
ROOMCbo3()
Showrecord()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Txt_Id.Focus()
ClereAllData()
End Sub
Private Sub ClereAllData()
Txt_Id.Text = ""
Cbo1.Text = ""
Cbo2.Text = ""
Cbo3.Text = ""
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim cmd As OleDbCommand = Nothing
Dim sqlDelete As String = ""
sqlDelete = "DELETE FROM BK_ROOM_R WHERE BRR_ROOM_CODE = '" & Txt_Id.Text & "'"
If Txt_Id.Text = "" Then
MessageBox.Show("กรุณากรอกข้อมูล", "คำเตือน", MessageBoxButtons.OK, MessageBoxIcon.Information)
Txt_Id.Focus()
Exit Sub
ElseIf MessageBox.Show("ทำการลบรหัส" & Txt_Id.Text & " ชื่อ " & Txt_Id.Text & " ", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
Exit Sub
End If
Try
cmd = New OleDbCommand(sqlDelete, OBCon)
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Finally
ClereAllData()
Showrecord()
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
ClereAllData()
End Sub
Private Sub dgv1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv1.CellContentClick
End Sub
Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter
End Sub
Private Sub Cbo1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cbo1.SelectedIndexChanged
LoadTllLvlRoom()
LoadTCntLvlRoom()
txtTllLvlRoom.Text = TllLvlRoom
txtCntLvlRoom.Text = CntLvlRoom
End Sub
Private Sub Txt_Id_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Txt_Id.TextChanged
End Sub
Private Sub Txt_Id_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Txt_Id.KeyPress
If Asc(e.KeyChar) = 13 Then
If (Txt_Id.Text = "") Then
Txt_Id.Focus()
Else
Cbo1.Focus()
End If
End If
End Sub
Private Sub Cbo1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Cbo1.KeyPress
If Asc(e.KeyChar) = 13 Then
If (Cbo1.Text = "") Then
Cbo1.Focus()
Else
Cbo2.Focus()
End If
End If
End Sub
Private Sub Cbo2_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Cbo2.KeyPress
If Asc(e.KeyChar) = 13 Then
If (Cbo2.Text = "") Then
Cbo2.Focus()
Else
Cbo3.Focus()
End If
End If
End Sub
Private Sub Cbo3_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Cbo3.KeyPress
If Asc(e.KeyChar) = 13 Then
Button2_Click(sender, e)
End If
End Sub
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
End Sub
Private Sub Cbo3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cbo3.SelectedIndexChanged
End Sub
Private Sub Cbo2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cbo2.SelectedIndexChanged
End Sub
Private Sub txtTllLvlRoom_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTllLvlRoom.TextChanged
End Sub
End Class
Private Sub LoadTllLvlRoom(ByVal strID As String)
Dim strsql As String
strsql = "select blr_numroom, blr_room_level from bk_level_r where blr_room_level = '"&strID &"'
Private Sub Cbo2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cbo2.SelectedIndexChanged
SqlTxt = "SELECT * from table where เงื่อนไข = cbo2.selectvalue "
OBCom = New OleDbCommand()
Dim DDR As DbDataReader
Dim DTB As DataTable
datagirdview.DataSource = DTB