Imports System.Data
Imports System.Data.OleDb
Public Class FrmConnection
Inherits System.Windows.Forms.Form
Dim dbConn As OleDbConnection
Dim dtAdapter As OleDbDataAdapter
Dim cmdBuilder As OleDbCommandBuilder
Dim dtSet As DataSet
Dim dtTable As DataTable
Dim objCurrencyMgr As CurrencyManager
Dim currRecPos As Integer
Dim fdname As String
Dim addnewMode As Boolean
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dbConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;" & "Password="""";Data Source=C:\Keep_book[v1.1]\KeepBook[v1_1]\KeepBook[v1_1]\book.mdb;")
dbConn.Open()
dtAdapter = New OleDbDataAdapter("SELECT * FROM Book_detail", dbConn)
dtSet = New DataSet("Book_detail")
dtAdapter.Fill(dtSet, "dataname")
dtTable = dtSet.Tables("dataname")
objCurrencyMgr = Me.BindingContext(dtTable, "")
cmdBuilder = New OleDbCommandBuilder(dtAdapter) ' คำสั่งนี้ต้องเรียกก่อน dtAdapter.Fill
mtxID.DataBindings.Add("text", dtTable, "ID")
mtxISBN10.DataBindings.Add("text", dtTable, "ISBN-10")
mtxISBN13.DataBindings.Add("text", dtTable, "ISBN-13")
txBookname.DataBindings.Add("text", dtTable, "Book_name")
txAuthor.DataBindings.Add("text", dtTable, "Author")
txPublis.DataBindings.Add("text", dtTable, "Publishers")
txPublisadd.DataBindings.Add("text", dtTable, "Publishers_address")
txDistri.DataBindings.Add("text", dtTable, "Distributor")
txDistriadd.DataBindings.Add("text", dtTable, "Distributor_address")
txPrintnum.DataBindings.Add("text", dtTable, "Print_Number")
txPrintamou.DataBindings.Add("text", dtTable, "Print_amount")
txPrintdate.DataBindings.Add("text", dtTable, "Print_date")
txBookprice.DataBindings.Add("text", dtTable, "Book_price")
txMainamou.DataBindings.Add("text", dtTable, "Maintain_amount")
txMaindate.DataBindings.Add("text", dtTable, "Maintain_date")
txMainID.DataBindings.Add("text", dtTable, "Maintain_ID")
txDonor.DataBindings.Add("text", dtTable, "Donor")
txDonorcon.DataBindings.Add("text", dtTable, "Donor_contact")
txStatus.DataBindings.Add("text", dtTable, "Status")
txNote.DataBindings.Add("text", dtTable, "Note")
DisableReadWrite()
AdjustPosition(sender, e)
AddHandler objCurrencyMgr.PositionChanged, AddressOf AdjustPosition
AddHandler Button2.Click, AddressOf AdjustPosition
AddHandler Button4.Click, AddressOf AdjustPosition
AddHandler Button9.Click, AddressOf AdjustPosition
AddHandler Button10.Click, AddressOf AdjustPosition
End Sub
Sub AutoGenearteMember_id()
Dim sqlTmp As String = ""
Dim comTmp As OleDbCommand = New OleDbCommand
Dim drTmp As OleDbDataReader
Dim tmpMember_id As Integer = 0
sqlTmp = "SELECT TOP 1 ID FROM Book_detail ORDER BY ID"
Try
With comTmp
.CommandType = CommandType.Text
.CommandText = sqlTmp 'ใช้ชุดคำสั่ง sql ที่เก็บอยู่ในตัวแปร sqlTmp
.Connection = dbConn 'ใช้การเชื่อมต่อของออบเจ็กต์ Connection
drTmp = .ExecuteReader 'รันชุดคำสั่ง SQl เก็บผลการทำงานไว้ที่ drTmp
drTmp.Read() 'อ่านข้อมูลในออบเจ็กต์ drTmp
'อ่านข้อมูลจากฟิลต์ Member_id เก็บไว้ในตัวแปร tmpMember_id
tmpMember_id = CInt(drTmp.Item("ID"))
drTmp.Close() 'ปิดออบเจ็กต์ drTmp
tmpMember_id = tmpMember_id + 1 'เพิ่มค่าอีก 1
txNote.Text = tmpMember_id.ToString("0000000") 'แสดงรหัสสมาชิกการที่สร้างได้
End With
Catch ex As Exception 'ในกรณีที่ไม่มีจำนวนเร็คคอร์ดอยู่เลย
txNote.Text = "0000001" 'กำหนดรหัสสมาชิกเท่ากับ 0001
End Try
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
'ปุ่มปิดโปรแกรม
If MessageBox.Show("ต้องการปิดโปรแกรม ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Me.Close()
End If
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
'ปุ่มดูทั้งหมด
'dtTable.Clear()
'dtAdapter.Fill(dtSet)
'DisableReadWrite()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'ปุ่มเพิ่มหนังสือ
addnewMode = True
currRecPos = objCurrencyMgr.Position
objCurrencyMgr.AddNew()
EnabledReadWrite()
txNote.Focus()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'ปุ่มลบ
If dtTable.Rows.Count <= 0 Then
MessageBox.Show("ไม่มีข้อมูลอยู่ในระเบียน", "ยืนยัน", MessageBoxButtons.OK)
Exit Sub
ElseIf MessageBox.Show("ต้องการลบระเบียนนี้หรือไม่ ?", "ยืนยันการลบระเบียน", MessageBoxButtons.YesNo) = vbYes Then
objCurrencyMgr.RemoveAt(objCurrencyMgr.Position)
dtAdapter.Update(dtTable)
currRecPos = objCurrencyMgr.Position
End If
If dtTable.Rows.Count = 0 Then
DisableReadWrite()
End If
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
'ปุ่มบันทึก
Try
objCurrencyMgr.EndCurrentEdit()
If txBookname.Text = " " Then
MessageBox.Show("ต้องระบุชื่อหนังสือถึงจะทำการบันทึกได้", "เตือน !", MessageBoxButtons.OK)
txBookname.Focus()
End If
cmdBuilder = New OleDbCommandBuilder(dtAdapter)
dtAdapter.Update(dtTable)
If addnewMode Then
objCurrencyMgr.AddNew()
End If
MessageBox.Show("บันทึกข้อมูลแล้ว", "Save Data", MessageBoxButtons.OK)
Catch ErrFromSave As Exception
MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้!" & ErrFromSave.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub DisableReadWrite()
mtxISBN10.ReadOnly = False
mtxISBN13.ReadOnly = False
txBookname.ReadOnly = False
txAuthor.ReadOnly = False
txPublis.ReadOnly = False
txPublisadd.ReadOnly = False
txDistri.ReadOnly = False
txDistriadd.ReadOnly = False
txPrintnum.ReadOnly = False
txPrintamou.ReadOnly = False
'txPrintdate.ReadOnly = False
txBookprice.ReadOnly = False
txMainamou.ReadOnly = False
'txMaindate.ReadOnly = False
txMainID.ReadOnly = False
txDonor.ReadOnly = False
txDonorcon.ReadOnly = False
txStatus.ReadOnly = False
txNote.ReadOnly = False
End Sub
Private Sub EnabledReadWrite()
mtxISBN10.ReadOnly = True
mtxISBN13.ReadOnly = True
txBookname.ReadOnly = True
txAuthor.ReadOnly = True
txPublis.ReadOnly = True
txPublisadd.ReadOnly = True
txDistri.ReadOnly = True
txDistriadd.ReadOnly = True
txPrintnum.ReadOnly = True
txPrintamou.ReadOnly = True
'txPrintdate.ReadOnly =
txBookprice.ReadOnly = True
txMainamou.ReadOnly = True
'txMaindate.ReadOnly = False
txMainID.ReadOnly = True
txDonor.ReadOnly = True
txDonorcon.ReadOnly = True
txStatus.ReadOnly = True
txNote.ReadOnly = True
End Sub
Private Sub mtxID_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles mtxID.KeyPress
If e.KeyChar = Chr(13) Then
mtxISBN10.Focus()
End If
End Sub
Private Sub mtxISBN10_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles mtxISBN10.KeyPress
If e.KeyChar = Chr(13) Then
mtxISBN13.Focus()
End If
End Sub
Private Sub mtxISBN13_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles mtxISBN13.KeyPress
If e.KeyChar = Chr(13) Then
txBookname.Focus()
End If
End Sub
Private Sub txBookname_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txBookname.KeyPress
If e.KeyChar = Chr(13) Then
txAuthor.Focus()
End If
End Sub
Private Sub txAuthor_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txAuthor.KeyPress
If e.KeyChar = Chr(13) Then
txPublis.Focus()
End If
End Sub
Private Sub txPublis_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txPublis.KeyPress
If e.KeyChar = Chr(13) Then
txPublisadd.Focus()
End If
End Sub
Private Sub txPublisadd_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txPublisadd.KeyPress
If e.KeyChar = Chr(13) Then
txDistri.Focus()
End If
End Sub
Private Sub txDistri_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txDistri.KeyPress
If e.KeyChar = Chr(13) Then
txDistriadd.Focus()
End If
End Sub
Private Sub txDistriadd_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txDistriadd.KeyPress
If e.KeyChar = Chr(13) Then
txPrintnum.Focus()
End If
End Sub
Private Sub txPrintnum_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txPrintnum.KeyPress
If e.KeyChar = Chr(13) Then
txPrintamou.Focus()
End If
End Sub
Private Sub txPrintamou_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txPrintamou.KeyPress
If e.KeyChar = Chr(13) Then
txPrintdate.Focus()
End If
End Sub
Private Sub txPrintdate_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txPrintdate.KeyPress
If e.KeyChar = Chr(13) Then
txBookprice.Focus()
End If
End Sub
Private Sub txBookprice_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txBookprice.KeyPress
If e.KeyChar = Chr(13) Then
txMainamou.Focus()
End If
End Sub
Private Sub txMainamou_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txMainamou.KeyPress
If e.KeyChar = Chr(13) Then
txMaindate.Focus()
End If
End Sub
Private Sub txMaindate_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txMaindate.KeyPress
If e.KeyChar = Chr(13) Then
txMainID.Focus()
End If
End Sub
Private Sub txMainID_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txMainID.KeyPress
If e.KeyChar = Chr(13) Then
txDonor.Focus()
End If
End Sub
Private Sub txDonor_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txDonor.KeyPress
If e.KeyChar = Chr(13) Then
txDonorcon.Focus()
End If
End Sub
Private Sub txDonorcon_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txDonorcon.KeyPress
If e.KeyChar = Chr(13) Then
txStatus.Focus()
End If
End Sub
Private Sub txStatus_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txStatus.KeyPress
If e.KeyChar = Chr(13) Then
txNote.Focus()
End If
End Sub
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
objCurrencyMgr.Position = 0
End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
objCurrencyMgr.Position -= 1
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
objCurrencyMgr.Position += 1
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
objCurrencyMgr.Position = objCurrencyMgr.Count - 1
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
'ปุ่มค้นหาข้อมูล
If addnewMode Then
Exit Sub
ElseIf TextBox1.Text <> "" Then
Dim i As Integer
For i = 0 To objCurrencyMgr.Count - 1
If Not dtTable.Rows(i)(fdname) Is DBNull.Value Then
If UCase(dtTable.Rows(i)(fdname)) Like "*" & UCase(TextBox1.Text) & "*" Then
Exit For
End If
End If
Next i
If i = objCurrencyMgr.Count Then
MessageBox.Show(" ไม่พบข้อมูลที่ต้องการ ", " ผลการค้นหา ", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
objCurrencyMgr.Position = i
End If
End If
TextBox1.Clear()
End Sub
Private Sub Button10_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button10.Click
If addnewMode Then
Exit Sub
ElseIf TextBox1.Text <> " " Then
Dim dtAdapter2 As OleDbDataAdapter = New OleDbDataAdapter( _
" SELECT * FROM Book_detail WHERE " & fdname & " LIKE'" & "%" & TextBox1.Text & "%'", dbConn)
dtSet.Clear()
dtAdapter2.Fill(dtSet)
dtTable = dtSet.Tables(0)
If dtTable.Rows.Count = 0 Then MessageBox.Show(" ไม่พบข้อมูลที่ต้องการ ", " ผลการค้นหา ", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
#Region "Custom Procedures"
Private Sub AdjustPosition(ByVal sender As System.Object, ByVal e As System.EventArgs)
IbIDisplayPos.Text = "ระเบียน " & objCurrencyMgr.Position + 1 & "/" & objCurrencyMgr.Count()
AddHandler Button1.Click, AddressOf SetButton
AddHandler Button2.Click, AddressOf SetButton
AddHandler Button4.Click, AddressOf SetButton
End Sub
Private Sub SetButton(ByVal sender As System.Object, ByVal e As System.EventArgs)
If addnewMode Then
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False
Button6.Enabled = False
btnFirst.Enabled = False
btnPrevious.Enabled = False
btnNext.Enabled = False
btnLast.Enabled = False
TextBox1.Text = ""
TextBox1.Enabled = False
Else
Button1.Enabled = True
Button2.Enabled = True
Button3.Enabled = True
Button6.Enabled = True
btnFirst.Enabled = True
btnPrevious.Enabled = True
btnNext.Enabled = True
btnLast.Enabled = True
TextBox1.Enabled = True
End If
End Sub
Private Sub CheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, CheckBox3.CheckedChanged, CheckBox4.CheckedChanged, CheckBox5.CheckedChanged
Select Case CInt(sender.Tag)
Case 0 : fdname = "ISBN-10"
Case 1 : fdname = "ISBN-13"
Case 2 : fdname = "Book_name"
Case 3 : fdname = "Author"
Case 4 : fdname = "Maintain_ID"
End Select
If addnewMode Then
TextBox1.Enabled = False
Else : TextBox1.Enabled = True
End If
TextBox1.Text = " "
TextBox1.Focus()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If dtTable.Rows.Count = 0 Then
Exit Sub
Else
addnewMode = False
EnabledReadWrite()
currRecPos = objCurrencyMgr.Position
txBookname.Focus()
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
objCurrencyMgr.CancelCurrentEdit()
If addnewMode Then
addnewMode = False
dtTable.Clear()
dtAdapter.Fill(dtSet)
If dtTable.Rows.Count = 0 Then
DisableReadWrite()
End If
End If
End Sub
#End Region
End Class
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
'ปุ่มบันทึก
Try
objCurrencyMgr.EndCurrentEdit()
If txBookname.Text = " " Then
MessageBox.Show("ต้องระบุชื่อหนังสือถึงจะทำการบันทึกได้", "เตือน !", MessageBoxButtons.OK)
txBookname.Focus()
End If
cmdBuilder = New OleDbCommandBuilder(dtAdapter)
dtAdapter.Update(dtTable)
If addnewMode Then
objCurrencyMgr.AddNew()
End If
MessageBox.Show("บันทึกข้อมูลแล้ว", "Save Data", MessageBoxButtons.OK)
Catch ErrFromSave As Exception
MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้!" & ErrFromSave.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub