If DS.Tables(0).Rows.Count >= 1 Then
StrMsg = "คุณต้องการลบข้อมูล [ " & DS.Tables(0).Rows(posi)("id_em") & "] นี้หรือไม่"
Else
MsgBox("ไม่สามารถลบข้อมูลได้เนื่องจากไม่มีข้อมูลใดๆในฐานข้อมูล")
Exit Sub
End If
Imports System.Data.OleDb
Imports System.Data
Module MdIDB
Public conn As New OleDbConnection
Public DA As OleDbDataAdapter
Public DS As DataSet
Public Update As DataSet
Public acscmd As New OleDbCommand
Public DSUsername As DataSet
Public DStb_employee As DataSet
Public DSemplo As DataSet
Public StrConn As String = "Provider=microsoft.ACE.oledb.12.0; data source= |datadirectory|\data.accdb"
Public StrSQL As String
Public LoginSuccess As Boolean
Public Sub OpenDB(ByVal StrSQL1 As String)
conn = New OleDbConnection(StrConn)
DA = New OleDbDataAdapter(StrSQL1, conn)
DS = New DataSet
DA.Fill(DS, "usergroup")
If DS.Tables(0).Rows.Count > 0 Then
LoginSuccess = True
Else
LoginSuccess = False
End If
End Sub
Public Sub OpenDBusergroup(ByVal StrSQL1 As String)
conn = New OleDbConnection(StrConn)
DA = New OleDbDataAdapter(StrSQL1, conn)
DS = New DataSet
DA.Fill(DS, "usergroup")
If DS.Tables(0).Rows.Count > 0 Then
LoginSuccess = True
Else
LoginSuccess = False
End If
End Sub
Public Function seekusergroup(ByVal StrSQL12 As String)
conn = New OleDbConnection(StrConn)
DA = New OleDbDataAdapter(StrSQL12, conn)
DS = New DataSet
DA.Fill(DS, "usergroup")
If DS.Tables(0).Rows.Count > 0 Then
Return True
Else
Return False
End If
End Function
Public Sub OpenDBtb_status(ByVal StrSQL1 As String)
conn = New OleDbConnection(StrConn)
DA = New OleDbDataAdapter(StrSQL1, conn)
DS = New DataSet
DA.Fill(DS, "tb_status")
If DS.Tables(0).Rows.Count > 0 Then
LoginSuccess = True
Else
LoginSuccess = False
End If
End Sub
Public Function seektb_status(ByVal StrSQL12 As String)
conn = New OleDbConnection(StrConn)
DA = New OleDbDataAdapter(StrSQL12, conn)
DS = New DataSet
DA.Fill(DS, "tb_status")
If DS.Tables(0).Rows.Count > 0 Then
Return True
Else
Return False
End If
End Function
Public Sub OpenDBtb_department(ByVal StrSQL1 As String)
conn = New OleDbConnection(StrConn)
DA = New OleDbDataAdapter(StrSQL1, conn)
DS = New DataSet
DA.Fill(DS, "tb_department")
If DS.Tables(0).Rows.Count > 0 Then
LoginSuccess = True
Else
LoginSuccess = False
End If
End Sub
Public Function seektb_department(ByVal StrSQL12 As String)
conn = New OleDbConnection(StrConn)
DA = New OleDbDataAdapter(StrSQL12, conn)
DS = New DataSet
DA.Fill(DS, "tb_department")
If DS.Tables(0).Rows.Count > 0 Then
Return True
Else
Return False
End If
End Function
Public Sub OpenDBtb_employee(ByVal StrSQL1 As String)
conn = New OleDbConnection(StrConn)
DA = New OleDbDataAdapter(StrSQL1, conn)
DS = New DataSet
DA.Fill(DS, "tb_employee")
If DS.Tables(0).Rows.Count > 0 Then
LoginSuccess = True
Else
LoginSuccess = False
End If
End Sub
Public Function seektb_employee(ByVal StrSQL12 As String)
conn = New OleDbConnection(StrConn)
DA = New OleDbDataAdapter(StrSQL12, conn)
DS = New DataSet
DA.Fill(DS, "tb_employee")
If DS.Tables(0).Rows.Count > 0 Then
Return True
Else
Return False
End If
End Function
Public Sub OpenDBtb_employee1(ByVal StrSQL1 As String)
conn = New OleDbConnection(StrConn)
DA = New OleDbDataAdapter(StrSQL1, conn)
DStb_employee = New DataSet
DA.Fill(DStb_employee, "tb_employee")
If DStb_employee.Tables(0).Rows.Count > 0 Then
LoginSuccess = True
Else
LoginSuccess = False
End If
End Sub
Public Function seektb_employee1(ByVal StrSQL12 As String)
conn = New OleDbConnection(StrConn)
DA = New OleDbDataAdapter(StrSQL12, conn)
DStb_employee = New DataSet
DA.Fill(DStb_employee, "tb_employee")
If DStb_employee.Tables(0).Rows.Count > 0 Then
Return True
Else
Return False
End If
End Function
End Module
Imports System.Data.OleDb
Imports System.Data
Public Class f_employee_2
Dim posi As Int32 = 0
Dim Status As String
Public conn As New OleDbConnection
Public Sub ShowDataUsergroup()
tid.Text = DS.Tables("tb_employee").Rows(posi)("id_em")
tcard.Text = DS.Tables("tb_employee").Rows(posi)("card_em")
tfront.Text = DS.Tables("tb_employee").Rows(posi)("front_em")
tfname.Text = DS.Tables("tb_employee").Rows(posi)("fname_em")
tlname.Text = DS.Tables("tb_employee").Rows(posi)("lname_em")
tbdate.Text = DS.Tables("tb_employee").Rows(posi)("bdate_em")
taddress.Text = DS.Tables("tb_employee").Rows(posi)("address_em")
tprovince.Text = DS.Tables("tb_employee").Rows(posi)("province_em")
ttel.Text = DS.Tables("tb_employee").Rows(posi)("tel_em")
tmobile.Text = DS.Tables("tb_employee").Rows(posi)("mobile_em")
temail.Text = DS.Tables("tb_employee").Rows(posi)("email_em")
tdepartment.Text = DS.Tables("tb_employee").Rows(posi)("department_em")
tfunction.Text = DS.Tables("tb_employee").Rows(posi)("function_em")
tposition.Text = DS.Tables("tb_employee").Rows(posi)("position_em")
End Sub
Public Sub connectData()
Dim s As String
If conn.State = ConnectionState.Open Then conn.Close()
s = StrConn
conn.ConnectionString = s
conn.Open()
End Sub
Private Sub f_employee(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show("ต้องการจบการทำงานหรือไม่", "ระบบ", MessageBoxButtons.YesNo) = DialogResult.No Then
' Cancel the Closing event from closing the form.
e.Cancel = True
End If
End Sub
Public Function executeReader(ByVal prmsql As String) As OleDbDataReader
Dim dr As OleDbDataReader
Dim comm As New OleDbCommand
connectData()
With comm
.CommandType = CommandType.Text
.CommandText = prmsql
.Connection = conn
dr = .ExecuteReader()
Return dr
End With
End Function
Public Function executesql(ByVal prmsql As String) As Boolean
connectData()
Dim addcom As New OleDbCommand
With addcom
.CommandType = CommandType.Text
.CommandText = prmsql
.Connection = conn
Try
.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message, vbOKOnly, "Error During DBCon.Open()")
executesql = False
Exit Function
End Try
End With
executesql = True
End Function
Private Sub f_employee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
loademployee()
End Sub
Private Sub loademployee()
Dim tposi As Integer
Dim pst As Integer
OpenDBtb_department("Select * From tb_department")
tposi = DS.Tables("tb_department").Rows.Count - 1
For pst = 0 To tposi
tdepartment.Items.Add(DS.Tables("tb_department").Rows(pst)("name_department"))
Next
OpenDBtb_status("Select * From tb_status")
tposi = DS.Tables("tb_status").Rows.Count - 1
For pst = 0 To tposi
tposition.Items.Add(DS.Tables("tb_status").Rows(pst)("status_full"))
Next
End Sub
Private Sub bsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bsearch.Click
DS = New DataSet
StrSQL = "select id_em,card_em,front_em,fname_em,lname_em,bdate_em,address_em,province_em,tel_em,mobile_em,email_em,department_em,function_em,position_em from tb_employee "
If Rid.Checked = True Then
StrSQL &= "WHERE id_em = '" & tsearch.Text & "'"
ElseIf Rcard.Checked = True Then
StrSQL &= "WHERE card_em = '" & tsearch.Text & "'"
ElseIf Rname.Checked = True Then
StrSQL &= "WHERE fname_em = '" & tsearch.Text & "'"
End If
'StrSQL &= " WHERE(id_person like '%" & tsearch.Text & "%')"
StrSQL &= " order by fname_em"
Try
With conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = StrConn
.Open()
End With
DA = New OleDb.OleDbDataAdapter(StrSQL, conn)
DA.Fill(DS, "tb_employee")
conn.Close()
If DS.Tables("tb_employee").Rows.Count > 0 Then
tid.Text = DS.Tables("tb_employee").Rows(posi)("id_em")
tcard.Text = DS.Tables("tb_employee").Rows(posi)("card_em")
tfront.Text = DS.Tables("tb_employee").Rows(posi)("front_em")
tfname.Text = DS.Tables("tb_employee").Rows(posi)("fname_em")
tlname.Text = DS.Tables("tb_employee").Rows(posi)("lname_em")
tbdate.Text = DS.Tables("tb_employee").Rows(posi)("bdate_em")
taddress.Text = DS.Tables("tb_employee").Rows(posi)("address_em")
tprovince.Text = DS.Tables("tb_employee").Rows(posi)("province_em")
ttel.Text = DS.Tables("tb_employee").Rows(posi)("tel_em")
tmobile.Text = DS.Tables("tb_employee").Rows(posi)("mobile_em")
temail.Text = DS.Tables("tb_employee").Rows(posi)("email_em")
tdepartment.Text = DS.Tables("tb_employee").Rows(posi)("department_em")
tfunction.Text = DS.Tables("tb_employee").Rows(posi)("function_em")
tposition.Text = DS.Tables("tb_employee").Rows(posi)("position_em")
DS.Tables("tb_employee").Rows.Clear()
Else
MsgBox("ไม่พบข้อมูลของ :" + tsearch.Text)
End If
Catch ex As Exception
End Try
End Sub
Private Sub bexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bexit.Click
Me.Close()
End Sub
Private Sub bcancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bcancel.Click
tid.Text = ""
tcard.Text = ""
tfront.Text = ""
tfname.Text = ""
tlname.Text = ""
tbdate.Text = ""
taddress.Text = ""
tprovince.Text = ""
ttel.Text = ""
tmobile.Text = ""
temail.Text = ""
tdepartment.Text = ""
tfunction.Text = ""
tposition.Text = ""
End Sub
Private Sub bdelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bdelete.Click
Dim MsgResult As MsgBoxResult
Dim StrMsg As String
If DS.Tables(0).Rows.Count >= 1 Then
StrMsg = "คุณต้องการลบข้อมูล [ " & DS.Tables(0).Rows(posi)("id_em") & " ] นี้หรือไม่"
Else
MsgBox("ไม่สามารถลบข้อมูลได้เนื่องจากไม่มีข้อมูลใดๆในฐานข้อมูล")
Exit Sub
End If
MsgResult = MsgBox(StrMsg, MsgBoxStyle.YesNoCancel + MsgBoxStyle.Critical, "em System")
If MsgResult = MsgBoxResult.Yes Then
If DS.Tables(0).Rows.Count >= 2 Then
Dim cmb As New OleDb.OleDbCommandBuilder(DA)
DS.Tables("tb_employee").Rows(posi).Delete()
DA.Update(DS, "tb_employee")
MsgBox("ลบข้อมูลเรียบร้อยแล้ว !!!")
posi = 0
ShowDataUsergroup()
ElseIf DS.Tables(0).Rows.Count = 1 Then
Dim cmb As New OleDb.OleDbCommandBuilder(DA)
DS.Tables("tb_employee").Rows(posi).Delete()
DA.Update(DS, "tb_employee")
MsgBox("ลบข้อมูลเรียบร้อยแล้ว!!!")
ElseIf DS.Tables(0).Rows.Count <= 0 Then
MsgBox("ไม่สามารถลบข้อมูลได้เนื่องจากไม่มีข้อมูลใดๆในฐานข้อมูล")
Exit Sub
End If
End If
End Sub
End Class
Imports System.Data.OleDb
Imports System.Data
Module MdIDB
Public conn As New OleDbConnection
Public DA As OleDbDataAdapter
Public DS As DataSet
Public Update As DataSet
Public acscmd As New OleDbCommand
Public DSUsername As DataSet
Public StrConn As String = "Provider=microsoft.ACE.oledb.12.0; data source= |datadirectory|\data.accdb"
Public StrSQL As String
Public LoginSuccess As Boolean
Public Sub OpenDB(ByVal StrSQL1 As String)
conn = New OleDbConnection(StrConn)
DA = New OleDbDataAdapter(StrSQL1, conn)
DS = New DataSet
DA.Fill(DS, "usergroup")
If DS.Tables(0).Rows.Count > 0 Then
LoginSuccess = True
Else
LoginSuccess = False
End If
End Sub
Public Sub OpenDBusergroup(ByVal StrSQL1 As String)
conn = New OleDbConnection(StrConn)
DA = New OleDbDataAdapter(StrSQL1, conn)
DS = New DataSet
DA.Fill(DS, "usergroup")
If DS.Tables(0).Rows.Count > 0 Then
LoginSuccess = True
Else
LoginSuccess = False
End If
End Sub
Public Function seekusergroup(ByVal StrSQL12 As String)
conn = New OleDbConnection(StrConn)
DA = New OleDbDataAdapter(StrSQL12, conn)
DS = New DataSet
DA.Fill(DS, "usergroup")
If DS.Tables(0).Rows.Count > 0 Then
Return True
Else
Return False
End If
End Function
Public Sub OpenDBtb_status(ByVal StrSQL1 As String)
conn = New OleDbConnection(StrConn)
DA = New OleDbDataAdapter(StrSQL1, conn)
DS = New DataSet
DA.Fill(DS, "tb_status")
If DS.Tables(0).Rows.Count > 0 Then
LoginSuccess = True
Else
LoginSuccess = False
End If
End Sub
Public Function seektb_status(ByVal StrSQL12 As String)
conn = New OleDbConnection(StrConn)
DA = New OleDbDataAdapter(StrSQL12, conn)
DS = New DataSet
DA.Fill(DS, "tb_status")
If DS.Tables(0).Rows.Count > 0 Then
Return True
Else
Return False
End If
End Function
Public Sub OpenDBtb_department(ByVal StrSQL1 As String)
conn = New OleDbConnection(StrConn)
DA = New OleDbDataAdapter(StrSQL1, conn)
DS = New DataSet
DA.Fill(DS, "tb_department")
If DS.Tables(0).Rows.Count > 0 Then
LoginSuccess = True
Else
LoginSuccess = False
End If
End Sub
Public Function seektb_department(ByVal StrSQL12 As String)
conn = New OleDbConnection(StrConn)
DA = New OleDbDataAdapter(StrSQL12, conn)
DS = New DataSet
DA.Fill(DS, "tb_department")
If DS.Tables(0).Rows.Count > 0 Then
Return True
Else
Return False
End If
End Function
Public Sub OpenDBtb_employee(ByVal StrSQL1 As String)
conn = New OleDbConnection(StrConn)
DA = New OleDbDataAdapter(StrSQL1, conn)
DS = New DataSet
DA.Fill(DS, "tb_employee")
If DS.Tables(0).Rows.Count > 0 Then
LoginSuccess = True
Else
LoginSuccess = False
End If
End Sub
Public Function seektb_employee(ByVal StrSQL12 As String)
conn = New OleDbConnection(StrConn)
DA = New OleDbDataAdapter(StrSQL12, conn)
DS = New DataSet
DA.Fill(DS, "tb_employee")
If DS.Tables(0).Rows.Count > 0 Then
Return True
Else
Return False
End If
End Function
End Module
Private Sub BDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BDelete.Click
Dim MsgResult As MsgBoxResult
Dim StrMsg As String
If DS.Tables(0).Rows.Count >= 1 Then
StrMsg = "คุณต้องการลบข้อมูล [ " & DS.Tables(0).Rows(posi)("status_sort") & " ] นี้หรือไม่"
Else
MsgBox("ไม่สามารถลบข้อมูลได้ เนื่องจาก ไม่มีข้อมูลใด ๆ ในฐานข้อมูล ")
Exit Sub
End If
MsgResult = MsgBox(StrMsg, MsgBoxStyle.YesNoCancel + MsgBoxStyle.Critical, "sru resert System")
If MsgResult = MsgBoxResult.Yes Then
If DS.Tables(0).Rows.Count >= 2 Then
Dim dcmb As New OleDb.OleDbCommandBuilder(DA)
DS.Tables("tb_status").Rows(posi).Delete()
DA.Update(DS, "tb_status")
MsgBox("ลบข้อมูลเรียบร้อยแล้ว !!!")
posi = 0
ShowDataUsergroup()
ElseIf DS.Tables(0).Rows.Count = 1 Then
Dim d2cmb As New OleDb.OleDbCommandBuilder(DA)
DS.Tables("tb_status").Rows(posi).Delete()
DA.Update(DS, "tb_status")
MsgBox("ลบข้อมูลเรียบร้อยแล้ว !!!")
ElseIf DS.Tables(0).Rows.Count <= 0 Then
MsgBox("ไม่สามารถลบข้อมูลได้ เนื่องจาก ไม่มีข้อมูลใด ๆ ในฐานข้อมูล ")
Exit Sub
End If
End If
End Sub
ElseIf DS.Tables(0).Rows.Count = 1 Then
Dim d2cmb As New OleDb.OleDbCommandBuilder(DA)
DS.Tables("tb_status").Rows(posi).Delete()
DA.Update(DS, "tb_status")
MsgBox("ลบข้อมูลเรียบร้อยแล้ว !!!")
ไม่ใจนะ ไม่เคยเขียนแบบนี้ตรงนี้ผิดหรือเปล่า มันหา id ที่จะลบไม่เจอ
ขอดูปุ่มอัพเดทหน่อยดิ
ผมว่ามันน่าจะเขียนประมาณนี้นะ
Code
Delete
FROM tb_employee
WHERE id_em = '" & tid.Text & "'