Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Inherits System.Windows.Forms.Form
Dim dbConn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\azi\book.mdb")
Dim dtAdapter As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM Book_detail", dbConn)
Dim cmdBuilder As OleDbCommandBuilder
Dim dtSet As DataSet = New DataSet()
Dim dtTable As DataTable
Dim objCurrencyMgr As CurrencyManager
Dim currRecPos As Integer
Dim addnewMode As Boolean
Dim fdname As String
Dim sqlTmp As String = ""
Dim comTmp As OleDbCommand = New OleDbCommand
Dim drTmp As OleDbDataReader
Dim tmpMember_id As Integer = 0
Sub AutoGenearteMember_id()
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
txID.Text = tmpMember_id.ToString("0000") 'แสดงรหัสสมาชิกการที่สร้างได้
End With
Catch ex As Exception 'ในกรณีที่ไม่มีจำนวนเร็คคอร์ดอยู่เลย
txID.Text = "0001" 'กำหนดรหัสสมาชิกเท่ากับ 0001
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dtAdapter.Fill(dtSet)
dtTable = dtSet.Tables(0)
objCurrencyMgr = Me.BindingContext(dtTable)
cmdBuilder = New OleDbCommandBuilder(dtAdapter)
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
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()
mtxISBN10.Focus()
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
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
#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 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 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
Imports System.Data.OleDb
Imports System.Data
Public Class FrmConnection
Inherits System.Windows.Forms.Form
Dim dbConn As OleDbConnection
Dim dbcmd As OleDbCommand
Dim dtSet As DataSet
Dim dtTable As DataTable
Dim dtAdapter As OleDbDataAdapter
Dim objCurrencyMgr As CurrencyManager
Dim cmdBuilder As OleDbCommandBuilder
Dim s As String
Dim addnewMode As Boolean
Dim fdname As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'BookDataSet.Book_detail' table. You can move, or remove it, as needed.
Me.Book_detailTableAdapter.Fill(Me.BookDataSet.Book_detail)
dbConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;" & "Password="""";Data Source=C:\ion\aziKeep\aziKeep\book.mdb;")
dbConn.Open()
dtAdapter = New OleDbDataAdapter("SELECT * FROM Book_detail", dbConn)
dtSet = New DataSet("Book_detail")
cmdBuilder = New OleDbCommandBuilder(dtAdapter) ' คำสั่งนี้ต้องเรียกก่อน dtAdapter.Fill
dtAdapter.Fill(dtSet, "dataname")
dtTable = dtSet.Tables("dataname")
objCurrencyMgr = Me.BindingContext(dtTable, "")
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")
txBookprice.DataBindings.Add("text", dtTable, "Book_price")
txMaintain.DataBindings.Add("text", dtTable, "Maintain_amount")
mtxMaintaindate.DataBindings.Add("text", dtTable, "Maintain_date")
txMaintainID.DataBindings.Add("text", dtTable, "Maintain_ID")
txStatus.DataBindings.Add("text", dtTable, "Status")
txNote.DataBindings.Add("text", dtTable, "Note")
DisableReadWrite()
AdjustPosition(sender, e)
AddHandler objCurrencyMgr.PositionChanged, AddressOf AdjustPosition
AddHandler btnDelete.Click, AddressOf AdjustPosition
AddHandler btnCancel.Click, AddressOf AdjustPosition
AddHandler btnFind.Click, AddressOf AdjustPosition
AddHandler btnFill.Click, AddressOf AdjustPosition
Label17.Text = objCurrencyMgr.Count()
End Sub
Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
dbConn.Close()
If MessageBox.Show("ต้องการปิดโปรแกรม ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Me.Close()
End If
End Sub