Imports System.Data
Imports System.Data.OleDb
Public Class Rests
Dim status As String
Public sendVar As String
Public Sub Showdata()
Dim Con As OleDbConnection
Dim dtAdapter As OleDbDataAdapter
Dim dt As New DataTable
Dim Str As String
Str = "SELECT * FROM Rest,Student,Patients WHERE Rest.Student_ID = Student.Student_ID AND Rest.Patients_ID = Patients.Patients_ID"
Connection(Con)
dtAdapter = New OleDbDataAdapter(Str, Con)
dtAdapter.Fill(dt)
Con.Close()
dtAdapter = Nothing
Con = Nothing
End Sub
Private Sub add_Student()
Dim objConn As New OleDbConnection
Dim dtAdapter As OleDbDataAdapter
Dim ds As New DataSet
Dim strSQL As String
Connection(objConn)
strSQL = "SELECT * FROM Student"
dtAdapter = New OleDbDataAdapter(strSQL, objConn)
dtAdapter.Fill(ds)
With TextBox1.Text
End With
TextBox1.Text = 1
End Sub
Private Sub add_Bag()
Dim objConn As New OleDbConnection
Dim dtAdapter As OleDbDataAdapter
Dim ds As New DataSet
Dim strSQL As String
Connection(objConn)
strSQL = "SELECT * FROM Patients"
dtAdapter = New OleDbDataAdapter(strSQL, objConn)
dtAdapter.Fill(ds)
With ComboBox1
.DataSource = ds.Tables(0)
.DisplayMember = "Patients_name"
.ValueMember = "Patients_ID"
End With
ComboBox1.SelectedItem = 1
End Sub
Public Sub Disable()
TextBox1.Enabled = False
TextBox2.Enabled = False
ComboBox1.Enabled = False
DateTimePicker1.Enabled = False
End Sub
Public Sub Enable()
TextBox2.Enabled = True
ComboBox1.Enabled = True
DateTimePicker1.Enabled = True
End Sub
Public Sub Clear()
TextBox2.Text = ""
TextBox1.Text = ""
ComboBox1.Text = ""
DateTimePicker1.Text = ""
End Sub
Private Sub Test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Disable()
Showdata()
add_Bag()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Enable()
status = "add"
End Sub
Public Shared OID As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Clear()
Disable()
status = ""
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim Conn As New OleDbConnection
Dim Cmd As New OleDbCommand
Dim Str As String = ""
If status = "add" Then
Str = "INSERT INTO Rest(Student_ID,Patients_ID,Date_of_rest) VALUES("
Str &= "" & TextBox2.Text & ","
Str &= "'" & ComboBox1.SelectedValue.ToString & "', "
Str &= "'" & DateTimePicker1.Text & "'"
Str &= ")"
ElseIf status = "edit" Then
Str = "UPDATE Rest SET "
Str &= "Student_ID=" & TextBox2.Text & ","
Str &= "Patients_ID=" & ComboBox1.SelectedValue.ToString & ","
Str &= "Date_of_rest='" & DateTimePicker1.Text & "'"
Str &= "WHERE Date_of_rest=" & TextBox1.Text & ""
ElseIf status = "del" Then
Str = "DELETE Rest WHERE Rest_ID=" & TextBox1.Text & ""
End If
Try
Connection(Conn)
Cmd = New OleDbCommand(Str, Conn)
Cmd.ExecuteNonQuery()
Conn.Close()
Catch ex As Exception
MsgBox("Error")
End Try
Showdata()
Clear()
Disable()
status = ""
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
status = "del"
If MsgBox("แน่ใจหรือไม่ว่าต้องการลบข้อมูล", MsgBoxStyle.OkCancel, "ตกลง") = MsgBoxResult.Ok Then
Dim Conn As New OleDbConnection
Dim Cmd As New OleDbCommand
Dim Str As String = "DELETE FROM Rest WHERE Rest_ID=" & TextBox1.Text & ""
Connection(Conn)
Cmd = New OleDbCommand(Str, Conn)
Cmd.ExecuteNonQuery()
Conn.Close()
Showdata()
Clear()
Disable()
status = ""
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
status = "edit"
Enable()
TextBox1.Enabled = False
End Sub
Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
TextBox1.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString
TextBox2.Text = DataGridView1.Rows(e.RowIndex).Cells(1).Value.ToString
ComboBox1.Text = DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString
DateTimePicker1.Text = DataGridView1.Rows(e.RowIndex).Cells(3).Value.ToString
End Sub
End Class